Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Copyright (C) 2003 PathScale, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Licensed under the GPL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/personality.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <asm/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <asm/ucontext.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <frame_kern.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <skas.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * FPU tag word conversions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	unsigned int tmp; /* to avoid 16 bit prefixes in the code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	/* Transform each pair of bits into 01 (valid) or 00 (empty) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	tmp = ~twd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	/* and move the valid bits to the lower byte. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	return tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static inline unsigned long twd_fxsr_to_i387(struct user_fxsr_struct *fxsave)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct _fpxreg *st = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	unsigned long twd = (unsigned long) fxsave->twd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	unsigned long tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	unsigned long ret = 0xffff0000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define FPREG_ADDR(f, n)	((char *)&(f)->st_space + (n) * 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	for (i = 0; i < 8; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		if (twd & 0x1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			st = (struct _fpxreg *) FPREG_ADDR(fxsave, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			switch (st->exponent & 0x7fff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			case 0x7fff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 				tag = 2;		/* Special */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			case 0x0000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 				if ( !st->significand[0] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 				     !st->significand[1] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 				     !st->significand[2] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 				     !st->significand[3] ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 					tag = 1;	/* Zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 					tag = 2;	/* Special */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 				if (st->significand[3] & 0x8000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 					tag = 0;	/* Valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 					tag = 2;	/* Special */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			tag = 3;			/* Empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		ret |= (tag << (2 * i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		twd = twd >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static int convert_fxsr_to_user(struct _fpstate __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 				struct user_fxsr_struct *fxsave)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	unsigned long env[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct _fpreg __user *to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct _fpxreg *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	env[2] = twd_fxsr_to_i387(fxsave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	env[3] = fxsave->fip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	env[5] = fxsave->foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	env[6] = fxsave->fos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (__copy_to_user(buf, env, 7 * sizeof(unsigned long)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	to = &buf->_st[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	from = (struct _fpxreg *) &fxsave->st_space[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	for (i = 0; i < 8; i++, to++, from++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		unsigned long __user *t = (unsigned long __user *)to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		unsigned long *f = (unsigned long *)from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		if (__put_user(*f, t) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				__put_user(*(f + 1), t + 1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				__put_user(from->exponent, &to->exponent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int convert_fxsr_from_user(struct user_fxsr_struct *fxsave,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 				  struct _fpstate __user *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	unsigned long env[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct _fpxreg *to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	struct _fpreg __user *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (copy_from_user( env, buf, 7 * sizeof(long)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	fxsave->cwd = (unsigned short)(env[0] & 0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	fxsave->swd = (unsigned short)(env[1] & 0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	fxsave->fip = env[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	fxsave->fcs = (env[4] & 0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	fxsave->foo = env[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	fxsave->fos = env[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	to = (struct _fpxreg *) &fxsave->st_space[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	from = &buf->_st[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	for (i = 0; i < 8; i++, to++, from++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		unsigned long *t = (unsigned long *)to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		unsigned long __user *f = (unsigned long __user *)from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (__get_user(*t, f) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		    __get_user(*(t + 1), f + 1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		    __get_user(to->exponent, &from->exponent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) extern int have_fpx_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int copy_sc_from_user(struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			     struct sigcontext __user *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct sigcontext sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	int err, pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	/* Always make any pending restarted system calls return -EINTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	current->restart_block.fn = do_no_restart_syscall;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	err = copy_from_user(&sc, from, sizeof(sc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #define GETREG(regno, regname) regs->regs.gp[HOST_##regno] = sc.regname
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	GETREG(GS, gs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	GETREG(FS, fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	GETREG(ES, es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	GETREG(DS, ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	GETREG(DI, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	GETREG(SI, si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	GETREG(BP, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	GETREG(SP, sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	GETREG(BX, bx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	GETREG(DX, dx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	GETREG(CX, cx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	GETREG(AX, ax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	GETREG(IP, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) #ifdef CONFIG_X86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	GETREG(R8, r8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	GETREG(R9, r9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	GETREG(R10, r10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	GETREG(R11, r11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	GETREG(R12, r12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	GETREG(R13, r13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	GETREG(R14, r14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	GETREG(R15, r15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	GETREG(CS, cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	GETREG(EFLAGS, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	GETREG(SS, ss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) #undef GETREG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	pid = userspace_pid[current_thread_info()->cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (have_fpx_regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		struct user_fxsr_struct fpx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		err = copy_from_user(&fpx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			&((struct _fpstate __user *)sc.fpstate)->_fxsr_env[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 				     sizeof(struct user_fxsr_struct));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		err = convert_fxsr_from_user(&fpx, (void *)sc.fpstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		err = restore_fpx_registers(pid, (unsigned long *) &fpx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			printk(KERN_ERR "copy_sc_from_user - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			       "restore_fpx_registers failed, errno = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			       -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		err = copy_from_user(regs->regs.fp, (void *)sc.fpstate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				     sizeof(struct _xstate));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static int copy_sc_to_user(struct sigcontext __user *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			   struct _xstate __user *to_fp, struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			   unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct sigcontext sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct faultinfo * fi = &current->thread.arch.faultinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	int err, pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	memset(&sc, 0, sizeof(struct sigcontext));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) #define PUTREG(regno, regname) sc.regname = regs->regs.gp[HOST_##regno]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	PUTREG(GS, gs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	PUTREG(FS, fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	PUTREG(ES, es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	PUTREG(DS, ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	PUTREG(DI, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	PUTREG(SI, si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	PUTREG(BP, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	PUTREG(SP, sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	PUTREG(BX, bx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	PUTREG(DX, dx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	PUTREG(CX, cx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	PUTREG(AX, ax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) #ifdef CONFIG_X86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	PUTREG(R8, r8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	PUTREG(R9, r9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	PUTREG(R10, r10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	PUTREG(R11, r11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	PUTREG(R12, r12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	PUTREG(R13, r13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	PUTREG(R14, r14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	PUTREG(R15, r15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	sc.cr2 = fi->cr2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	sc.err = fi->error_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	sc.trapno = fi->trap_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	PUTREG(IP, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	PUTREG(CS, cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	PUTREG(EFLAGS, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	PUTREG(SP, sp_at_signal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	PUTREG(SS, ss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) #undef PUTREG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	sc.oldmask = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	sc.fpstate = (unsigned long)to_fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	err = copy_to_user(to, &sc, sizeof(struct sigcontext));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	pid = userspace_pid[current_thread_info()->cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (have_fpx_regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		struct user_fxsr_struct fpx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		err = save_fpx_registers(pid, (unsigned long *) &fpx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if (err < 0){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			printk(KERN_ERR "copy_sc_to_user - save_fpx_registers "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			       "failed, errno = %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		err = convert_fxsr_to_user(&to_fp->fpstate, &fpx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		err |= __put_user(fpx.swd, &to_fp->fpstate.status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		err |= __put_user(X86_FXSR_MAGIC, &to_fp->fpstate.magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		if (copy_to_user(&to_fp->fpstate._fxsr_env[0], &fpx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 				 sizeof(struct user_fxsr_struct)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		if (copy_to_user(to_fp, regs->regs.fp, sizeof(struct _xstate)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int copy_ucontext_to_user(struct ucontext __user *uc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 				 struct _xstate __user *fp, sigset_t *set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 				 unsigned long sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	err |= __save_altstack(&uc->uc_stack, sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct sigframe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	char __user *pretcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	int sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	struct sigcontext sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	struct _xstate fpstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	unsigned long extramask[_NSIG_WORDS-1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	char retcode[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct rt_sigframe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	char __user *pretcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	int sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	struct siginfo __user *pinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	void __user *puc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	struct siginfo info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	struct ucontext uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	struct _xstate fpstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	char retcode[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) int setup_signal_stack_sc(unsigned long stack_top, struct ksignal *ksig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			  struct pt_regs *regs, sigset_t *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	struct sigframe __user *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	void __user *restorer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	int err = 0, sig = ksig->sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	/* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	stack_top = ((stack_top + 4) & -16UL) - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	frame = (struct sigframe __user *) stack_top - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (!access_ok(frame, sizeof(*frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	restorer = frame->retcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		restorer = ksig->ka.sa.sa_restorer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	err |= __put_user(restorer, &frame->pretcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	err |= __put_user(sig, &frame->sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	err |= copy_sc_to_user(&frame->sc, &frame->fpstate, regs, mask->sig[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (_NSIG_WORDS > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		err |= __copy_to_user(&frame->extramask, &mask->sig[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 				      sizeof(frame->extramask));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	 * This is popl %eax ; movl $,%eax ; int $0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	 * reasons and because gdb uses it as a signature to notice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	 * signal handler stack frames.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	PT_REGS_SP(regs) = (unsigned long) frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	PT_REGS_AX(regs) = (unsigned long) sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	PT_REGS_DX(regs) = (unsigned long) 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	PT_REGS_CX(regs) = (unsigned long) 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			  struct pt_regs *regs, sigset_t *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	struct rt_sigframe __user *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	void __user *restorer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	int err = 0, sig = ksig->sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	stack_top &= -8UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	frame = (struct rt_sigframe __user *) stack_top - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	if (!access_ok(frame, sizeof(*frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	restorer = frame->retcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		restorer = ksig->ka.sa.sa_restorer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	err |= __put_user(restorer, &frame->pretcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	err |= __put_user(sig, &frame->sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	err |= __put_user(&frame->info, &frame->pinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	err |= __put_user(&frame->uc, &frame->puc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 					PT_REGS_SP(regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	 * This is movl $,%eax ; int $0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	 * reasons and because gdb uses it as a signature to notice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	 * signal handler stack frames.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	PT_REGS_SP(regs) = (unsigned long) frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	PT_REGS_AX(regs) = (unsigned long) sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	PT_REGS_DX(regs) = (unsigned long) &frame->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	PT_REGS_CX(regs) = (unsigned long) &frame->uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) long sys_sigreturn(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	unsigned long sp = PT_REGS_SP(&current->thread.regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	sigset_t set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	struct sigcontext __user *sc = &frame->sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	if (copy_from_user(&set.sig[0], &sc->oldmask, sizeof(set.sig[0])) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	    copy_from_user(&set.sig[1], frame->extramask, sig_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		goto segfault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	set_current_blocked(&set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (copy_sc_from_user(&current->thread.regs, sc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		goto segfault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	/* Avoid ERESTART handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	return PT_REGS_SYSCALL_RET(&current->thread.regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)  segfault:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	force_sig(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct rt_sigframe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	char __user *pretcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	struct ucontext uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	struct siginfo info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	struct _xstate fpstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			  struct pt_regs *regs, sigset_t *set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	struct rt_sigframe __user *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	int err = 0, sig = ksig->sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	unsigned long fp_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	frame = (struct rt_sigframe __user *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		round_down(stack_top - sizeof(struct rt_sigframe), 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	/* Subtract 128 for a red zone and 8 for proper alignment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (!access_ok(frame, sizeof(*frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		err |= copy_siginfo_to_user(&frame->info, &ksig->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	/* Create the ucontext.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	err |= __put_user(0, &frame->uc.uc_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	err |= __put_user(0, &frame->uc.uc_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	err |= __save_altstack(&frame->uc.uc_stack, PT_REGS_SP(regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	err |= copy_sc_to_user(&frame->uc.uc_mcontext, &frame->fpstate, regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			       set->sig[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	fp_to = (unsigned long)&frame->fpstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	err |= __put_user(fp_to, &frame->uc.uc_mcontext.fpstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (sizeof(*set) == 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		err |= __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		err |= __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		err |= __copy_to_user(&frame->uc.uc_sigmask, set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 				      sizeof(*set));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	 * Set up to return from userspace.  If provided, use a stub
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	 * already in userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	/* x86-64 should always use SA_RESTORER. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		err |= __put_user((void *)ksig->ka.sa.sa_restorer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 				  &frame->pretcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		/* could use a vstub here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	PT_REGS_SP(regs) = (unsigned long) frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	PT_REGS_DI(regs) = sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	/* In case the signal handler was declared without prototypes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	PT_REGS_AX(regs) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	 * This also works for non SA_SIGINFO handlers because they expect the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	 * next argument after the signal number on the stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	PT_REGS_SI(regs) = (unsigned long) &frame->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	PT_REGS_DX(regs) = (unsigned long) &frame->uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	PT_REGS_IP(regs) = (unsigned long) ksig->ka.sa.sa_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) long sys_rt_sigreturn(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	unsigned long sp = PT_REGS_SP(&current->thread.regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	struct rt_sigframe __user *frame =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		(struct rt_sigframe __user *)(sp - sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	struct ucontext __user *uc = &frame->uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	sigset_t set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	if (copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		goto segfault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	set_current_blocked(&set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	if (copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		goto segfault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	/* Avoid ERESTART handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	return PT_REGS_SYSCALL_RET(&current->thread.regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)  segfault:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	force_sig(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }