Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
0003  * Licensed under the GPL
0004  */
0005 
0006 #include <linux/mm.h>
0007 #include <linux/sched.h>
0008 #include <linux/uaccess.h>
0009 #include <asm/ptrace-abi.h>
0010 #include <registers.h>
0011 #include <skas.h>
0012 
0013 extern int arch_switch_tls(struct task_struct *to);
0014 
0015 void arch_switch_to(struct task_struct *to)
0016 {
0017     int err = arch_switch_tls(to);
0018     if (!err)
0019         return;
0020 
0021     if (err != -EINVAL)
0022         printk(KERN_WARNING "arch_switch_tls failed, errno %d, "
0023                "not EINVAL\n", -err);
0024     else
0025         printk(KERN_WARNING "arch_switch_tls failed, errno = EINVAL\n");
0026 }
0027 
0028 int is_syscall(unsigned long addr)
0029 {
0030     unsigned short instr;
0031     int n;
0032 
0033     n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
0034     if (n) {
0035         /* access_process_vm() grants access to vsyscall and stub,
0036          * while copy_from_user doesn't. Maybe access_process_vm is
0037          * slow, but that doesn't matter, since it will be called only
0038          * in case of singlestepping, if copy_from_user failed.
0039          */
0040         n = access_process_vm(current, addr, &instr, sizeof(instr),
0041                 FOLL_FORCE);
0042         if (n != sizeof(instr)) {
0043             printk(KERN_ERR "is_syscall : failed to read "
0044                    "instruction from 0x%lx\n", addr);
0045             return 1;
0046         }
0047     }
0048     /* int 0x80 or sysenter */
0049     return (instr == 0x80cd) || (instr == 0x340f);
0050 }
0051 
0052 /* determines which flags the user has access to. */
0053 /* 1 = access 0 = no access */
0054 #define FLAG_MASK 0x00044dd5
0055 
0056 static const int reg_offsets[] = {
0057     [EBX] = HOST_BX,
0058     [ECX] = HOST_CX,
0059     [EDX] = HOST_DX,
0060     [ESI] = HOST_SI,
0061     [EDI] = HOST_DI,
0062     [EBP] = HOST_BP,
0063     [EAX] = HOST_AX,
0064     [DS] = HOST_DS,
0065     [ES] = HOST_ES,
0066     [FS] = HOST_FS,
0067     [GS] = HOST_GS,
0068     [EIP] = HOST_IP,
0069     [CS] = HOST_CS,
0070     [EFL] = HOST_EFLAGS,
0071     [UESP] = HOST_SP,
0072     [SS] = HOST_SS,
0073     [ORIG_EAX] = HOST_ORIG_AX,
0074 };
0075 
0076 int putreg(struct task_struct *child, int regno, unsigned long value)
0077 {
0078     regno >>= 2;
0079     switch (regno) {
0080     case EBX:
0081     case ECX:
0082     case EDX:
0083     case ESI:
0084     case EDI:
0085     case EBP:
0086     case EAX:
0087     case EIP:
0088     case UESP:
0089         break;
0090     case ORIG_EAX:
0091         /* Update the syscall number. */
0092         UPT_SYSCALL_NR(&child->thread.regs.regs) = value;
0093         break;
0094     case FS:
0095         if (value && (value & 3) != 3)
0096             return -EIO;
0097         break;
0098     case GS:
0099         if (value && (value & 3) != 3)
0100             return -EIO;
0101         break;
0102     case DS:
0103     case ES:
0104         if (value && (value & 3) != 3)
0105             return -EIO;
0106         value &= 0xffff;
0107         break;
0108     case SS:
0109     case CS:
0110         if ((value & 3) != 3)
0111             return -EIO;
0112         value &= 0xffff;
0113         break;
0114     case EFL:
0115         value &= FLAG_MASK;
0116         child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
0117         return 0;
0118     default :
0119         panic("Bad register in putreg() : %d\n", regno);
0120     }
0121     child->thread.regs.regs.gp[reg_offsets[regno]] = value;
0122     return 0;
0123 }
0124 
0125 int poke_user(struct task_struct *child, long addr, long data)
0126 {
0127     if ((addr & 3) || addr < 0)
0128         return -EIO;
0129 
0130     if (addr < MAX_REG_OFFSET)
0131         return putreg(child, addr, data);
0132     else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
0133          (addr <= offsetof(struct user, u_debugreg[7]))) {
0134         addr -= offsetof(struct user, u_debugreg[0]);
0135         addr = addr >> 2;
0136         if ((addr == 4) || (addr == 5))
0137             return -EIO;
0138         child->thread.arch.debugregs[addr] = data;
0139         return 0;
0140     }
0141     return -EIO;
0142 }
0143 
0144 unsigned long getreg(struct task_struct *child, int regno)
0145 {
0146     unsigned long mask = ~0UL;
0147 
0148     regno >>= 2;
0149     switch (regno) {
0150     case FS:
0151     case GS:
0152     case DS:
0153     case ES:
0154     case SS:
0155     case CS:
0156         mask = 0xffff;
0157         break;
0158     case EIP:
0159     case UESP:
0160     case EAX:
0161     case EBX:
0162     case ECX:
0163     case EDX:
0164     case ESI:
0165     case EDI:
0166     case EBP:
0167     case EFL:
0168     case ORIG_EAX:
0169         break;
0170     default:
0171         panic("Bad register in getreg() : %d\n", regno);
0172     }
0173     return mask & child->thread.regs.regs.gp[reg_offsets[regno]];
0174 }
0175 
0176 /* read the word at location addr in the USER area. */
0177 int peek_user(struct task_struct *child, long addr, long data)
0178 {
0179     unsigned long tmp;
0180 
0181     if ((addr & 3) || addr < 0)
0182         return -EIO;
0183 
0184     tmp = 0;  /* Default return condition */
0185     if (addr < MAX_REG_OFFSET) {
0186         tmp = getreg(child, addr);
0187     }
0188     else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
0189          (addr <= offsetof(struct user, u_debugreg[7]))) {
0190         addr -= offsetof(struct user, u_debugreg[0]);
0191         addr = addr >> 2;
0192         tmp = child->thread.arch.debugregs[addr];
0193     }
0194     return put_user(tmp, (unsigned long __user *) data);
0195 }
0196 
0197 static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
0198 {
0199     int err, n, cpu = task_cpu(child);
0200     struct user_i387_struct fpregs;
0201 
0202     err = save_i387_registers(userspace_pid[cpu],
0203                   (unsigned long *) &fpregs);
0204     if (err)
0205         return err;
0206 
0207     n = copy_to_user(buf, &fpregs, sizeof(fpregs));
0208     if(n > 0)
0209         return -EFAULT;
0210 
0211     return n;
0212 }
0213 
0214 static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
0215 {
0216     int n, cpu = task_cpu(child);
0217     struct user_i387_struct fpregs;
0218 
0219     n = copy_from_user(&fpregs, buf, sizeof(fpregs));
0220     if (n > 0)
0221         return -EFAULT;
0222 
0223     return restore_i387_registers(userspace_pid[cpu],
0224                     (unsigned long *) &fpregs);
0225 }
0226 
0227 static int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
0228 {
0229     int err, n, cpu = task_cpu(child);
0230     struct user_fxsr_struct fpregs;
0231 
0232     err = save_fpx_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
0233     if (err)
0234         return err;
0235 
0236     n = copy_to_user(buf, &fpregs, sizeof(fpregs));
0237     if(n > 0)
0238         return -EFAULT;
0239 
0240     return n;
0241 }
0242 
0243 static int set_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
0244 {
0245     int n, cpu = task_cpu(child);
0246     struct user_fxsr_struct fpregs;
0247 
0248     n = copy_from_user(&fpregs, buf, sizeof(fpregs));
0249     if (n > 0)
0250         return -EFAULT;
0251 
0252     return restore_fpx_registers(userspace_pid[cpu],
0253                      (unsigned long *) &fpregs);
0254 }
0255 
0256 long subarch_ptrace(struct task_struct *child, long request,
0257             unsigned long addr, unsigned long data)
0258 {
0259     int ret = -EIO;
0260     void __user *datap = (void __user *) data;
0261     switch (request) {
0262     case PTRACE_GETFPREGS: /* Get the child FPU state. */
0263         ret = get_fpregs(datap, child);
0264         break;
0265     case PTRACE_SETFPREGS: /* Set the child FPU state. */
0266         ret = set_fpregs(datap, child);
0267         break;
0268     case PTRACE_GETFPXREGS: /* Get the child FPU state. */
0269         ret = get_fpxregs(datap, child);
0270         break;
0271     case PTRACE_SETFPXREGS: /* Set the child FPU state. */
0272         ret = set_fpxregs(datap, child);
0273         break;
0274     default:
0275         ret = -EIO;
0276     }
0277     return ret;
0278 }