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