11e5db223SRicardo Neri /* 29774a96fSBabu Moger * umip.c Emulation for instruction protected by the User-Mode Instruction 39774a96fSBabu Moger * Prevention feature 41e5db223SRicardo Neri * 51e5db223SRicardo Neri * Copyright (c) 2017, Intel Corporation. 61e5db223SRicardo Neri * Ricardo Neri <ricardo.neri-calderon@linux.intel.com> 71e5db223SRicardo Neri */ 81e5db223SRicardo Neri 91e5db223SRicardo Neri #include <linux/uaccess.h> 101e5db223SRicardo Neri #include <asm/umip.h> 111e5db223SRicardo Neri #include <asm/traps.h> 121e5db223SRicardo Neri #include <asm/insn.h> 131e5db223SRicardo Neri #include <asm/insn-eval.h> 14c6a960bbSRicardo Neri #include <linux/ratelimit.h> 15c6a960bbSRicardo Neri 16c6a960bbSRicardo Neri #undef pr_fmt 17c6a960bbSRicardo Neri #define pr_fmt(fmt) "umip: " fmt 181e5db223SRicardo Neri 191e5db223SRicardo Neri /** DOC: Emulation for User-Mode Instruction Prevention (UMIP) 201e5db223SRicardo Neri * 219774a96fSBabu Moger * User-Mode Instruction Prevention is a security feature present in recent 229774a96fSBabu Moger * x86 processors that, when enabled, prevents a group of instructions (SGDT, 239774a96fSBabu Moger * SIDT, SLDT, SMSW and STR) from being run in user mode by issuing a general 249774a96fSBabu Moger * protection fault if the instruction is executed with CPL > 0. 251e5db223SRicardo Neri * 261e5db223SRicardo Neri * Rather than relaying to the user space the general protection fault caused by 271e5db223SRicardo Neri * the UMIP-protected instructions (in the form of a SIGSEGV signal), it can be 281e5db223SRicardo Neri * trapped and emulate the result of such instructions to provide dummy values. 291e5db223SRicardo Neri * This allows to both conserve the current kernel behavior and not reveal the 301e5db223SRicardo Neri * system resources that UMIP intends to protect (i.e., the locations of the 311e5db223SRicardo Neri * global descriptor and interrupt descriptor tables, the segment selectors of 321e5db223SRicardo Neri * the local descriptor table, the value of the task state register and the 331e5db223SRicardo Neri * contents of the CR0 register). 341e5db223SRicardo Neri * 351e5db223SRicardo Neri * This emulation is needed because certain applications (e.g., WineHQ and 361e5db223SRicardo Neri * DOSEMU2) rely on this subset of instructions to function. 371e5db223SRicardo Neri * 381e5db223SRicardo Neri * The instructions protected by UMIP can be split in two groups. Those which 39e86c2c8bSBrendan Shanks * return a kernel memory address (SGDT and SIDT) and those which return a 40e86c2c8bSBrendan Shanks * value (SLDT, STR and SMSW). 411e5db223SRicardo Neri * 421e5db223SRicardo Neri * For the instructions that return a kernel memory address, applications 431e5db223SRicardo Neri * such as WineHQ rely on the result being located in the kernel memory space, 441e5db223SRicardo Neri * not the actual location of the table. The result is emulated as a hard-coded 451e5db223SRicardo Neri * value that, lies close to the top of the kernel memory. The limit for the GDT 461e5db223SRicardo Neri * and the IDT are set to zero. 471e5db223SRicardo Neri * 48b91e7089SBrendan Shanks * The instruction SMSW is emulated to return the value that the register CR0 491e5db223SRicardo Neri * has at boot time as set in the head_32. 50b91e7089SBrendan Shanks * SLDT and STR are emulated to return the values that the kernel programmatically 51b91e7089SBrendan Shanks * assigns: 52b91e7089SBrendan Shanks * - SLDT returns (GDT_ENTRY_LDT * 8) if an LDT has been set, 0 if not. 53b91e7089SBrendan Shanks * - STR returns (GDT_ENTRY_TSS * 8). 541e5db223SRicardo Neri * 55e86c2c8bSBrendan Shanks * Emulation is provided for both 32-bit and 64-bit processes. 561e5db223SRicardo Neri * 571e5db223SRicardo Neri * Care is taken to appropriately emulate the results when segmentation is 581e5db223SRicardo Neri * used. That is, rather than relying on USER_DS and USER_CS, the function 591e5db223SRicardo Neri * insn_get_addr_ref() inspects the segment descriptor pointed by the 601e5db223SRicardo Neri * registers in pt_regs. This ensures that we correctly obtain the segment 611e5db223SRicardo Neri * base address and the address and operand sizes even if the user space 621e5db223SRicardo Neri * application uses a local descriptor table. 631e5db223SRicardo Neri */ 641e5db223SRicardo Neri 65e86c2c8bSBrendan Shanks #define UMIP_DUMMY_GDT_BASE 0xfffffffffffe0000ULL 66e86c2c8bSBrendan Shanks #define UMIP_DUMMY_IDT_BASE 0xffffffffffff0000ULL 671e5db223SRicardo Neri 681e5db223SRicardo Neri /* 691e5db223SRicardo Neri * The SGDT and SIDT instructions store the contents of the global descriptor 701e5db223SRicardo Neri * table and interrupt table registers, respectively. The destination is a 711e5db223SRicardo Neri * memory operand of X+2 bytes. X bytes are used to store the base address of 72e86c2c8bSBrendan Shanks * the table and 2 bytes are used to store the limit. In 32-bit processes X 73e86c2c8bSBrendan Shanks * has a value of 4, in 64-bit processes X has a value of 8. 741e5db223SRicardo Neri */ 75e86c2c8bSBrendan Shanks #define UMIP_GDT_IDT_BASE_SIZE_64BIT 8 76e86c2c8bSBrendan Shanks #define UMIP_GDT_IDT_BASE_SIZE_32BIT 4 771e5db223SRicardo Neri #define UMIP_GDT_IDT_LIMIT_SIZE 2 781e5db223SRicardo Neri 791e5db223SRicardo Neri #define UMIP_INST_SGDT 0 /* 0F 01 /0 */ 801e5db223SRicardo Neri #define UMIP_INST_SIDT 1 /* 0F 01 /1 */ 816e2a3064SRicardo Neri #define UMIP_INST_SMSW 2 /* 0F 01 /4 */ 826e2a3064SRicardo Neri #define UMIP_INST_SLDT 3 /* 0F 00 /0 */ 836e2a3064SRicardo Neri #define UMIP_INST_STR 4 /* 0F 00 /1 */ 841e5db223SRicardo Neri 85b0e387c3SJason Yan static const char * const umip_insns[5] = { 86fd11a649SRicardo Neri [UMIP_INST_SGDT] = "SGDT", 87fd11a649SRicardo Neri [UMIP_INST_SIDT] = "SIDT", 88fd11a649SRicardo Neri [UMIP_INST_SMSW] = "SMSW", 89fd11a649SRicardo Neri [UMIP_INST_SLDT] = "SLDT", 90fd11a649SRicardo Neri [UMIP_INST_STR] = "STR", 91fd11a649SRicardo Neri }; 92fd11a649SRicardo Neri 93fd11a649SRicardo Neri #define umip_pr_err(regs, fmt, ...) \ 94fd11a649SRicardo Neri umip_printk(regs, KERN_ERR, fmt, ##__VA_ARGS__) 958d3bcc44SKefeng Wang #define umip_pr_warn(regs, fmt, ...) \ 96fd11a649SRicardo Neri umip_printk(regs, KERN_WARNING, fmt, ##__VA_ARGS__) 97fd11a649SRicardo Neri 98fd11a649SRicardo Neri /** 99fd11a649SRicardo Neri * umip_printk() - Print a rate-limited message 100fd11a649SRicardo Neri * @regs: Register set with the context in which the warning is printed 101fd11a649SRicardo Neri * @log_level: Kernel log level to print the message 102fd11a649SRicardo Neri * @fmt: The text string to print 103fd11a649SRicardo Neri * 104fd11a649SRicardo Neri * Print the text contained in @fmt. The print rate is limited to bursts of 5 105fd11a649SRicardo Neri * messages every two minutes. The purpose of this customized version of 106fd11a649SRicardo Neri * printk() is to print messages when user space processes use any of the 107fd11a649SRicardo Neri * UMIP-protected instructions. Thus, the printed text is prepended with the 108fd11a649SRicardo Neri * task name and process ID number of the current task as well as the 109fd11a649SRicardo Neri * instruction and stack pointers in @regs as seen when entering kernel mode. 110fd11a649SRicardo Neri * 111fd11a649SRicardo Neri * Returns: 112fd11a649SRicardo Neri * 113fd11a649SRicardo Neri * None. 114fd11a649SRicardo Neri */ 115fd11a649SRicardo Neri static __printf(3, 4) 116fd11a649SRicardo Neri void umip_printk(const struct pt_regs *regs, const char *log_level, 117fd11a649SRicardo Neri const char *fmt, ...) 118fd11a649SRicardo Neri { 119fd11a649SRicardo Neri /* Bursts of 5 messages every two minutes */ 120fd11a649SRicardo Neri static DEFINE_RATELIMIT_STATE(ratelimit, 2 * 60 * HZ, 5); 121fd11a649SRicardo Neri struct task_struct *tsk = current; 122fd11a649SRicardo Neri struct va_format vaf; 123fd11a649SRicardo Neri va_list args; 124fd11a649SRicardo Neri 125fd11a649SRicardo Neri if (!__ratelimit(&ratelimit)) 126fd11a649SRicardo Neri return; 127fd11a649SRicardo Neri 128fd11a649SRicardo Neri va_start(args, fmt); 129fd11a649SRicardo Neri vaf.fmt = fmt; 130fd11a649SRicardo Neri vaf.va = &args; 131fd11a649SRicardo Neri printk("%s" pr_fmt("%s[%d] ip:%lx sp:%lx: %pV"), log_level, tsk->comm, 132fd11a649SRicardo Neri task_pid_nr(tsk), regs->ip, regs->sp, &vaf); 133fd11a649SRicardo Neri va_end(args); 134fd11a649SRicardo Neri } 135fd11a649SRicardo Neri 1361e5db223SRicardo Neri /** 1371e5db223SRicardo Neri * identify_insn() - Identify a UMIP-protected instruction 1381e5db223SRicardo Neri * @insn: Instruction structure with opcode and ModRM byte. 1391e5db223SRicardo Neri * 1401e5db223SRicardo Neri * From the opcode and ModRM.reg in @insn identify, if any, a UMIP-protected 1411e5db223SRicardo Neri * instruction that can be emulated. 1421e5db223SRicardo Neri * 1431e5db223SRicardo Neri * Returns: 1441e5db223SRicardo Neri * 1451e5db223SRicardo Neri * On success, a constant identifying a specific UMIP-protected instruction that 1461e5db223SRicardo Neri * can be emulated. 1471e5db223SRicardo Neri * 1481e5db223SRicardo Neri * -EINVAL on error or when not an UMIP-protected instruction that can be 1491e5db223SRicardo Neri * emulated. 1501e5db223SRicardo Neri */ 1511e5db223SRicardo Neri static int identify_insn(struct insn *insn) 1521e5db223SRicardo Neri { 1531e5db223SRicardo Neri /* By getting modrm we also get the opcode. */ 1541e5db223SRicardo Neri insn_get_modrm(insn); 1551e5db223SRicardo Neri 1561e5db223SRicardo Neri if (!insn->modrm.nbytes) 1571e5db223SRicardo Neri return -EINVAL; 1581e5db223SRicardo Neri 1591e5db223SRicardo Neri /* All the instructions of interest start with 0x0f. */ 1601e5db223SRicardo Neri if (insn->opcode.bytes[0] != 0xf) 1611e5db223SRicardo Neri return -EINVAL; 1621e5db223SRicardo Neri 1631e5db223SRicardo Neri if (insn->opcode.bytes[1] == 0x1) { 1641e5db223SRicardo Neri switch (X86_MODRM_REG(insn->modrm.value)) { 1651e5db223SRicardo Neri case 0: 1661e5db223SRicardo Neri return UMIP_INST_SGDT; 1671e5db223SRicardo Neri case 1: 1681e5db223SRicardo Neri return UMIP_INST_SIDT; 1691e5db223SRicardo Neri case 4: 1701e5db223SRicardo Neri return UMIP_INST_SMSW; 1711e5db223SRicardo Neri default: 1721e5db223SRicardo Neri return -EINVAL; 1731e5db223SRicardo Neri } 1746e2a3064SRicardo Neri } else if (insn->opcode.bytes[1] == 0x0) { 1756e2a3064SRicardo Neri if (X86_MODRM_REG(insn->modrm.value) == 0) 1766e2a3064SRicardo Neri return UMIP_INST_SLDT; 1776e2a3064SRicardo Neri else if (X86_MODRM_REG(insn->modrm.value) == 1) 1786e2a3064SRicardo Neri return UMIP_INST_STR; 1796e2a3064SRicardo Neri else 1801e5db223SRicardo Neri return -EINVAL; 1816e2a3064SRicardo Neri } else { 1826e2a3064SRicardo Neri return -EINVAL; 1836e2a3064SRicardo Neri } 1841e5db223SRicardo Neri } 1851e5db223SRicardo Neri 1861e5db223SRicardo Neri /** 1871e5db223SRicardo Neri * emulate_umip_insn() - Emulate UMIP instructions and return dummy values 1881e5db223SRicardo Neri * @insn: Instruction structure with operands 1891e5db223SRicardo Neri * @umip_inst: A constant indicating the instruction to emulate 1901e5db223SRicardo Neri * @data: Buffer into which the dummy result is stored 1911e5db223SRicardo Neri * @data_size: Size of the emulated result 192e86c2c8bSBrendan Shanks * @x86_64: true if process is 64-bit, false otherwise 1931e5db223SRicardo Neri * 1941e5db223SRicardo Neri * Emulate an instruction protected by UMIP and provide a dummy result. The 1951e5db223SRicardo Neri * result of the emulation is saved in @data. The size of the results depends 1961e5db223SRicardo Neri * on both the instruction and type of operand (register vs memory address). 1971e5db223SRicardo Neri * The size of the result is updated in @data_size. Caller is responsible 1981e5db223SRicardo Neri * of providing a @data buffer of at least UMIP_GDT_IDT_BASE_SIZE + 1991e5db223SRicardo Neri * UMIP_GDT_IDT_LIMIT_SIZE bytes. 2001e5db223SRicardo Neri * 2011e5db223SRicardo Neri * Returns: 2021e5db223SRicardo Neri * 2031e5db223SRicardo Neri * 0 on success, -EINVAL on error while emulating. 2041e5db223SRicardo Neri */ 2051e5db223SRicardo Neri static int emulate_umip_insn(struct insn *insn, int umip_inst, 206e86c2c8bSBrendan Shanks unsigned char *data, int *data_size, bool x86_64) 2071e5db223SRicardo Neri { 2081e5db223SRicardo Neri if (!data || !data_size || !insn) 2091e5db223SRicardo Neri return -EINVAL; 2101e5db223SRicardo Neri /* 2111e5db223SRicardo Neri * These two instructions return the base address and limit of the 2121e5db223SRicardo Neri * global and interrupt descriptor table, respectively. According to the 2131e5db223SRicardo Neri * Intel Software Development manual, the base address can be 24-bit, 2141e5db223SRicardo Neri * 32-bit or 64-bit. Limit is always 16-bit. If the operand size is 2151e5db223SRicardo Neri * 16-bit, the returned value of the base address is supposed to be a 2161e5db223SRicardo Neri * zero-extended 24-byte number. However, it seems that a 32-byte number 2171e5db223SRicardo Neri * is always returned irrespective of the operand size. 2181e5db223SRicardo Neri */ 2191e5db223SRicardo Neri if (umip_inst == UMIP_INST_SGDT || umip_inst == UMIP_INST_SIDT) { 220e86c2c8bSBrendan Shanks u64 dummy_base_addr; 221e86c2c8bSBrendan Shanks u16 dummy_limit = 0; 222e86c2c8bSBrendan Shanks 2231e5db223SRicardo Neri /* SGDT and SIDT do not use registers operands. */ 2241e5db223SRicardo Neri if (X86_MODRM_MOD(insn->modrm.value) == 3) 2251e5db223SRicardo Neri return -EINVAL; 2261e5db223SRicardo Neri 2271e5db223SRicardo Neri if (umip_inst == UMIP_INST_SGDT) 2281e5db223SRicardo Neri dummy_base_addr = UMIP_DUMMY_GDT_BASE; 2291e5db223SRicardo Neri else 2301e5db223SRicardo Neri dummy_base_addr = UMIP_DUMMY_IDT_BASE; 2311e5db223SRicardo Neri 232e86c2c8bSBrendan Shanks /* 233e86c2c8bSBrendan Shanks * 64-bit processes use the entire dummy base address. 234e86c2c8bSBrendan Shanks * 32-bit processes use the lower 32 bits of the base address. 235e86c2c8bSBrendan Shanks * dummy_base_addr is always 64 bits, but we memcpy the correct 236e86c2c8bSBrendan Shanks * number of bytes from it to the destination. 237e86c2c8bSBrendan Shanks */ 238e86c2c8bSBrendan Shanks if (x86_64) 239e86c2c8bSBrendan Shanks *data_size = UMIP_GDT_IDT_BASE_SIZE_64BIT; 240e86c2c8bSBrendan Shanks else 241e86c2c8bSBrendan Shanks *data_size = UMIP_GDT_IDT_BASE_SIZE_32BIT; 2421e5db223SRicardo Neri 243e86c2c8bSBrendan Shanks memcpy(data + 2, &dummy_base_addr, *data_size); 244e86c2c8bSBrendan Shanks 245e86c2c8bSBrendan Shanks *data_size += UMIP_GDT_IDT_LIMIT_SIZE; 2461e5db223SRicardo Neri memcpy(data, &dummy_limit, UMIP_GDT_IDT_LIMIT_SIZE); 2471e5db223SRicardo Neri 248b91e7089SBrendan Shanks } else if (umip_inst == UMIP_INST_SMSW || umip_inst == UMIP_INST_SLDT || 249b91e7089SBrendan Shanks umip_inst == UMIP_INST_STR) { 250b91e7089SBrendan Shanks unsigned long dummy_value; 251b91e7089SBrendan Shanks 252b91e7089SBrendan Shanks if (umip_inst == UMIP_INST_SMSW) { 253b91e7089SBrendan Shanks dummy_value = CR0_STATE; 254b91e7089SBrendan Shanks } else if (umip_inst == UMIP_INST_STR) { 255b91e7089SBrendan Shanks dummy_value = GDT_ENTRY_TSS * 8; 256b91e7089SBrendan Shanks } else if (umip_inst == UMIP_INST_SLDT) { 257b91e7089SBrendan Shanks #ifdef CONFIG_MODIFY_LDT_SYSCALL 258b91e7089SBrendan Shanks down_read(¤t->mm->context.ldt_usr_sem); 259b91e7089SBrendan Shanks if (current->mm->context.ldt) 260b91e7089SBrendan Shanks dummy_value = GDT_ENTRY_LDT * 8; 261b91e7089SBrendan Shanks else 262b91e7089SBrendan Shanks dummy_value = 0; 263b91e7089SBrendan Shanks up_read(¤t->mm->context.ldt_usr_sem); 264b91e7089SBrendan Shanks #else 265b91e7089SBrendan Shanks dummy_value = 0; 266b91e7089SBrendan Shanks #endif 267b91e7089SBrendan Shanks } 2681e5db223SRicardo Neri 2691e5db223SRicardo Neri /* 270b91e7089SBrendan Shanks * For these 3 instructions, the number 2711e5db223SRicardo Neri * of bytes to be copied in the result buffer is determined 2721e5db223SRicardo Neri * by whether the operand is a register or a memory location. 2731e5db223SRicardo Neri * If operand is a register, return as many bytes as the operand 2741e5db223SRicardo Neri * size. If operand is memory, return only the two least 275*163b0991SIngo Molnar * significant bytes. 2761e5db223SRicardo Neri */ 2771e5db223SRicardo Neri if (X86_MODRM_MOD(insn->modrm.value) == 3) 2781e5db223SRicardo Neri *data_size = insn->opnd_bytes; 2791e5db223SRicardo Neri else 2801e5db223SRicardo Neri *data_size = 2; 2811e5db223SRicardo Neri 2821e5db223SRicardo Neri memcpy(data, &dummy_value, *data_size); 2831e5db223SRicardo Neri } else { 2841e5db223SRicardo Neri return -EINVAL; 2851e5db223SRicardo Neri } 2861e5db223SRicardo Neri 2871e5db223SRicardo Neri return 0; 2881e5db223SRicardo Neri } 2891e5db223SRicardo Neri 2901e5db223SRicardo Neri /** 291c6a960bbSRicardo Neri * force_sig_info_umip_fault() - Force a SIGSEGV with SEGV_MAPERR 292c6a960bbSRicardo Neri * @addr: Address that caused the signal 293c6a960bbSRicardo Neri * @regs: Register set containing the instruction pointer 294c6a960bbSRicardo Neri * 295c6a960bbSRicardo Neri * Force a SIGSEGV signal with SEGV_MAPERR as the error code. This function is 296c6a960bbSRicardo Neri * intended to be used to provide a segmentation fault when the result of the 297c6a960bbSRicardo Neri * UMIP emulation could not be copied to the user space memory. 298c6a960bbSRicardo Neri * 299c6a960bbSRicardo Neri * Returns: none 300c6a960bbSRicardo Neri */ 301c6a960bbSRicardo Neri static void force_sig_info_umip_fault(void __user *addr, struct pt_regs *regs) 302c6a960bbSRicardo Neri { 303c6a960bbSRicardo Neri struct task_struct *tsk = current; 304c6a960bbSRicardo Neri 305c6a960bbSRicardo Neri tsk->thread.cr2 = (unsigned long)addr; 306c6a960bbSRicardo Neri tsk->thread.error_code = X86_PF_USER | X86_PF_WRITE; 307c6a960bbSRicardo Neri tsk->thread.trap_nr = X86_TRAP_PF; 308c6a960bbSRicardo Neri 3092e1661d2SEric W. Biederman force_sig_fault(SIGSEGV, SEGV_MAPERR, addr); 310c6a960bbSRicardo Neri 311c6a960bbSRicardo Neri if (!(show_unhandled_signals && unhandled_signal(tsk, SIGSEGV))) 312c6a960bbSRicardo Neri return; 313c6a960bbSRicardo Neri 314fd11a649SRicardo Neri umip_pr_err(regs, "segfault in emulation. error%x\n", 315fd11a649SRicardo Neri X86_PF_USER | X86_PF_WRITE); 316c6a960bbSRicardo Neri } 317c6a960bbSRicardo Neri 318c6a960bbSRicardo Neri /** 3191e5db223SRicardo Neri * fixup_umip_exception() - Fixup a general protection fault caused by UMIP 3201e5db223SRicardo Neri * @regs: Registers as saved when entering the #GP handler 3211e5db223SRicardo Neri * 322e86c2c8bSBrendan Shanks * The instructions SGDT, SIDT, STR, SMSW and SLDT cause a general protection 323e86c2c8bSBrendan Shanks * fault if executed with CPL > 0 (i.e., from user space). This function fixes 324e86c2c8bSBrendan Shanks * the exception up and provides dummy results for SGDT, SIDT and SMSW; STR 325e86c2c8bSBrendan Shanks * and SLDT are not fixed up. 3261e5db223SRicardo Neri * 3271e5db223SRicardo Neri * If operands are memory addresses, results are copied to user-space memory as 3281e5db223SRicardo Neri * indicated by the instruction pointed by eIP using the registers indicated in 3291e5db223SRicardo Neri * the instruction operands. If operands are registers, results are copied into 3301e5db223SRicardo Neri * the context that was saved when entering kernel mode. 3311e5db223SRicardo Neri * 3321e5db223SRicardo Neri * Returns: 3331e5db223SRicardo Neri * 3341e5db223SRicardo Neri * True if emulation was successful; false if not. 3351e5db223SRicardo Neri */ 3361e5db223SRicardo Neri bool fixup_umip_exception(struct pt_regs *regs) 3371e5db223SRicardo Neri { 338172b75e5SJoerg Roedel int nr_copied, reg_offset, dummy_data_size, umip_inst; 3391e5db223SRicardo Neri /* 10 bytes is the maximum size of the result of UMIP instructions */ 3401e5db223SRicardo Neri unsigned char dummy_data[10] = { 0 }; 3411e5db223SRicardo Neri unsigned char buf[MAX_INSN_SIZE]; 342172b75e5SJoerg Roedel unsigned long *reg_addr; 3431e5db223SRicardo Neri void __user *uaddr; 3441e5db223SRicardo Neri struct insn insn; 3451e5db223SRicardo Neri 3461e5db223SRicardo Neri if (!regs) 3471e5db223SRicardo Neri return false; 3481e5db223SRicardo Neri 349172b75e5SJoerg Roedel nr_copied = insn_fetch_from_user(regs, buf); 3501e5db223SRicardo Neri 3511e5db223SRicardo Neri /* 352172b75e5SJoerg Roedel * The insn_fetch_from_user above could have failed if user code 353172b75e5SJoerg Roedel * is protected by a memory protection key. Give up on emulation 354172b75e5SJoerg Roedel * in such a case. Should we issue a page fault? 3551e5db223SRicardo Neri */ 3561e5db223SRicardo Neri if (!nr_copied) 3571e5db223SRicardo Neri return false; 3581e5db223SRicardo Neri 359172639d7SJoerg Roedel if (!insn_decode(&insn, regs, buf, nr_copied)) 3601e5db223SRicardo Neri return false; 3611e5db223SRicardo Neri 3621e5db223SRicardo Neri umip_inst = identify_insn(&insn); 3631e5db223SRicardo Neri if (umip_inst < 0) 3641e5db223SRicardo Neri return false; 3651e5db223SRicardo Neri 3668d3bcc44SKefeng Wang umip_pr_warn(regs, "%s instruction cannot be used by applications.\n", 367fd11a649SRicardo Neri umip_insns[umip_inst]); 368fd11a649SRicardo Neri 3698d3bcc44SKefeng Wang umip_pr_warn(regs, "For now, expensive software emulation returns the result.\n"); 370fd11a649SRicardo Neri 371e86c2c8bSBrendan Shanks if (emulate_umip_insn(&insn, umip_inst, dummy_data, &dummy_data_size, 372e86c2c8bSBrendan Shanks user_64bit_mode(regs))) 3731e5db223SRicardo Neri return false; 3741e5db223SRicardo Neri 3751e5db223SRicardo Neri /* 3761e5db223SRicardo Neri * If operand is a register, write result to the copy of the register 3771e5db223SRicardo Neri * value that was pushed to the stack when entering into kernel mode. 3781e5db223SRicardo Neri * Upon exit, the value we write will be restored to the actual hardware 3791e5db223SRicardo Neri * register. 3801e5db223SRicardo Neri */ 3811e5db223SRicardo Neri if (X86_MODRM_MOD(insn.modrm.value) == 3) { 3821e5db223SRicardo Neri reg_offset = insn_get_modrm_rm_off(&insn, regs); 3831e5db223SRicardo Neri 3841e5db223SRicardo Neri /* 3851e5db223SRicardo Neri * Negative values are usually errors. In memory addressing, 3861e5db223SRicardo Neri * the exception is -EDOM. Since we expect a register operand, 3871e5db223SRicardo Neri * all negative values are errors. 3881e5db223SRicardo Neri */ 3891e5db223SRicardo Neri if (reg_offset < 0) 3901e5db223SRicardo Neri return false; 3911e5db223SRicardo Neri 3921e5db223SRicardo Neri reg_addr = (unsigned long *)((unsigned long)regs + reg_offset); 3931e5db223SRicardo Neri memcpy(reg_addr, dummy_data, dummy_data_size); 3941e5db223SRicardo Neri } else { 3951e5db223SRicardo Neri uaddr = insn_get_addr_ref(&insn, regs); 3961e5db223SRicardo Neri if ((unsigned long)uaddr == -1L) 3971e5db223SRicardo Neri return false; 3981e5db223SRicardo Neri 3991e5db223SRicardo Neri nr_copied = copy_to_user(uaddr, dummy_data, dummy_data_size); 400c6a960bbSRicardo Neri if (nr_copied > 0) { 401c6a960bbSRicardo Neri /* 402c6a960bbSRicardo Neri * If copy fails, send a signal and tell caller that 403c6a960bbSRicardo Neri * fault was fixed up. 404c6a960bbSRicardo Neri */ 405c6a960bbSRicardo Neri force_sig_info_umip_fault(uaddr, regs); 406c6a960bbSRicardo Neri return true; 407c6a960bbSRicardo Neri } 4081e5db223SRicardo Neri } 4091e5db223SRicardo Neri 4101e5db223SRicardo Neri /* increase IP to let the program keep going */ 4111e5db223SRicardo Neri regs->ip += insn.length; 4121e5db223SRicardo Neri return true; 4131e5db223SRicardo Neri } 414