1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 4 */ 5 6 #include <stddef.h> 7 #include <unistd.h> 8 #include <errno.h> 9 #include <string.h> 10 #include <sys/mman.h> 11 #include <init.h> 12 #include <as-layout.h> 13 #include <mm_id.h> 14 #include <os.h> 15 #include <ptrace_user.h> 16 #include <registers.h> 17 #include <skas.h> 18 #include <sysdep/ptrace.h> 19 #include <sysdep/stub.h> 20 #include "../internal.h" 21 22 extern char batch_syscall_stub[], __syscall_stub_start[]; 23 24 static inline unsigned long *check_init_stack(struct mm_id * mm_idp, 25 unsigned long *stack) 26 { 27 if (stack == NULL) { 28 stack = (unsigned long *) mm_idp->stack + 2; 29 *stack = 0; 30 } 31 return stack; 32 } 33 34 static unsigned long syscall_regs[MAX_REG_NR]; 35 36 static int __init init_syscall_regs(void) 37 { 38 get_safe_registers(syscall_regs, NULL); 39 syscall_regs[REGS_IP_INDEX] = STUB_CODE + 40 ((unsigned long) batch_syscall_stub - 41 (unsigned long) __syscall_stub_start); 42 syscall_regs[REGS_SP_INDEX] = STUB_DATA; 43 44 return 0; 45 } 46 47 __initcall(init_syscall_regs); 48 49 static inline long do_syscall_stub(struct mm_id * mm_idp, void **addr) 50 { 51 int n, i; 52 long ret, offset; 53 unsigned long * data; 54 unsigned long * syscall; 55 int err, pid = mm_idp->u.pid; 56 57 n = ptrace_setregs(pid, syscall_regs); 58 if (n < 0) { 59 printk(UM_KERN_ERR "Registers - \n"); 60 for (i = 0; i < MAX_REG_NR; i++) 61 printk(UM_KERN_ERR "\t%d\t0x%lx\n", i, syscall_regs[i]); 62 panic("%s : PTRACE_SETREGS failed, errno = %d\n", 63 __func__, -n); 64 } 65 66 err = ptrace(PTRACE_CONT, pid, 0, 0); 67 if (err) 68 panic("Failed to continue stub, pid = %d, errno = %d\n", pid, 69 errno); 70 71 wait_stub_done(pid); 72 73 /* 74 * When the stub stops, we find the following values on the 75 * beginning of the stack: 76 * (long )return_value 77 * (long )offset to failed sycall-data (0, if no error) 78 */ 79 ret = *((unsigned long *) mm_idp->stack); 80 offset = *((unsigned long *) mm_idp->stack + 1); 81 if (offset) { 82 data = (unsigned long *)(mm_idp->stack + offset - STUB_DATA); 83 printk(UM_KERN_ERR "%s : ret = %ld, offset = %ld, data = %p\n", 84 __func__, ret, offset, data); 85 syscall = (unsigned long *)((unsigned long)data + data[0]); 86 printk(UM_KERN_ERR "%s: syscall %ld failed, return value = 0x%lx, expected return value = 0x%lx\n", 87 __func__, syscall[0], ret, syscall[7]); 88 printk(UM_KERN_ERR " syscall parameters: 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 89 syscall[1], syscall[2], syscall[3], 90 syscall[4], syscall[5], syscall[6]); 91 for (n = 1; n < data[0]/sizeof(long); n++) { 92 if (n == 1) 93 printk(UM_KERN_ERR " additional syscall data:"); 94 if (n % 4 == 1) 95 printk("\n" UM_KERN_ERR " "); 96 printk(" 0x%lx", data[n]); 97 } 98 if (n > 1) 99 printk("\n"); 100 } 101 else ret = 0; 102 103 *addr = check_init_stack(mm_idp, NULL); 104 105 return ret; 106 } 107 108 long run_syscall_stub(struct mm_id * mm_idp, int syscall, 109 unsigned long *args, long expected, void **addr, 110 int done) 111 { 112 unsigned long *stack = check_init_stack(mm_idp, *addr); 113 114 *stack += sizeof(long); 115 stack += *stack / sizeof(long); 116 117 *stack++ = syscall; 118 *stack++ = args[0]; 119 *stack++ = args[1]; 120 *stack++ = args[2]; 121 *stack++ = args[3]; 122 *stack++ = args[4]; 123 *stack++ = args[5]; 124 *stack++ = expected; 125 *stack = 0; 126 127 if (!done && ((((unsigned long) stack) & ~UM_KERN_PAGE_MASK) < 128 UM_KERN_PAGE_SIZE - 10 * sizeof(long))) { 129 *addr = stack; 130 return 0; 131 } 132 133 return do_syscall_stub(mm_idp, addr); 134 } 135 136 long syscall_stub_data(struct mm_id * mm_idp, 137 unsigned long *data, int data_count, 138 void **addr, void **stub_addr) 139 { 140 unsigned long *stack; 141 int ret = 0; 142 143 /* 144 * If *addr still is uninitialized, it *must* contain NULL. 145 * Thus in this case do_syscall_stub correctly won't be called. 146 */ 147 if ((((unsigned long) *addr) & ~UM_KERN_PAGE_MASK) >= 148 UM_KERN_PAGE_SIZE - (10 + data_count) * sizeof(long)) { 149 ret = do_syscall_stub(mm_idp, addr); 150 /* in case of error, don't overwrite data on stack */ 151 if (ret) 152 return ret; 153 } 154 155 stack = check_init_stack(mm_idp, *addr); 156 *addr = stack; 157 158 *stack = data_count * sizeof(long); 159 160 memcpy(stack + 1, data, data_count * sizeof(long)); 161 162 *stub_addr = (void *)(((unsigned long)(stack + 1) & 163 ~UM_KERN_PAGE_MASK) + STUB_DATA); 164 165 return 0; 166 } 167 168 int map(struct mm_id * mm_idp, unsigned long virt, unsigned long len, int prot, 169 int phys_fd, unsigned long long offset, int done, void **data) 170 { 171 int ret; 172 unsigned long args[] = { virt, len, prot, 173 MAP_SHARED | MAP_FIXED, phys_fd, 174 MMAP_OFFSET(offset) }; 175 176 ret = run_syscall_stub(mm_idp, STUB_MMAP_NR, args, virt, 177 data, done); 178 179 return ret; 180 } 181 182 int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len, 183 int done, void **data) 184 { 185 int ret; 186 unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0, 187 0 }; 188 189 ret = run_syscall_stub(mm_idp, __NR_munmap, args, 0, 190 data, done); 191 192 return ret; 193 } 194 195 int protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len, 196 unsigned int prot, int done, void **data) 197 { 198 int ret; 199 unsigned long args[] = { addr, len, prot, 0, 0, 0 }; 200 201 ret = run_syscall_stub(mm_idp, __NR_mprotect, args, 0, 202 data, done); 203 204 return ret; 205 } 206