197870c34SAlex Dewar // SPDX-License-Identifier: GPL-2.0
2f45d9fc9SGennady Sharapov /*
3ba180fd4SJeff Dike * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4f45d9fc9SGennady Sharapov */
5f45d9fc9SGennady Sharapov
6ba180fd4SJeff Dike #include <stddef.h>
7ba180fd4SJeff Dike #include <unistd.h>
8f45d9fc9SGennady Sharapov #include <errno.h>
9f45d9fc9SGennady Sharapov #include <string.h>
10f45d9fc9SGennady Sharapov #include <sys/mman.h>
1137185b33SAl Viro #include <init.h>
1237185b33SAl Viro #include <as-layout.h>
1337185b33SAl Viro #include <mm_id.h>
1437185b33SAl Viro #include <os.h>
1537185b33SAl Viro #include <ptrace_user.h>
1637185b33SAl Viro #include <registers.h>
1737185b33SAl Viro #include <skas.h>
1837185b33SAl Viro #include <sysdep/ptrace.h>
1937185b33SAl Viro #include <sysdep/stub.h>
20f45d9fc9SGennady Sharapov
215f32943bSNicolas Iooss extern char batch_syscall_stub[], __syscall_stub_start[];
22f45d9fc9SGennady Sharapov
2316dd07bcSJeff Dike extern void wait_stub_done(int pid);
24f45d9fc9SGennady Sharapov
check_init_stack(struct mm_id * mm_idp,unsigned long * stack)25f45d9fc9SGennady Sharapov static inline unsigned long *check_init_stack(struct mm_id * mm_idp,
26f45d9fc9SGennady Sharapov unsigned long *stack)
27f45d9fc9SGennady Sharapov {
28f45d9fc9SGennady Sharapov if (stack == NULL) {
29f45d9fc9SGennady Sharapov stack = (unsigned long *) mm_idp->stack + 2;
30f45d9fc9SGennady Sharapov *stack = 0;
31f45d9fc9SGennady Sharapov }
32f45d9fc9SGennady Sharapov return stack;
33f45d9fc9SGennady Sharapov }
34f45d9fc9SGennady Sharapov
3516dd07bcSJeff Dike static unsigned long syscall_regs[MAX_REG_NR];
3616dd07bcSJeff Dike
init_syscall_regs(void)3716dd07bcSJeff Dike static int __init init_syscall_regs(void)
3816dd07bcSJeff Dike {
39fbfe9c84SIngo van Lil get_safe_registers(syscall_regs, NULL);
4054ae36f2SJeff Dike syscall_regs[REGS_IP_INDEX] = STUB_CODE +
415f32943bSNicolas Iooss ((unsigned long) batch_syscall_stub -
425f32943bSNicolas Iooss (unsigned long) __syscall_stub_start);
439f0b4807SJohannes Berg syscall_regs[REGS_SP_INDEX] = STUB_DATA;
449f0b4807SJohannes Berg
4516dd07bcSJeff Dike return 0;
4616dd07bcSJeff Dike }
4716dd07bcSJeff Dike
4816dd07bcSJeff Dike __initcall(init_syscall_regs);
4916dd07bcSJeff Dike
do_syscall_stub(struct mm_id * mm_idp,void ** addr)50f45d9fc9SGennady Sharapov static inline long do_syscall_stub(struct mm_id * mm_idp, void **addr)
51f45d9fc9SGennady Sharapov {
52b92c4f92SJeff Dike int n, i;
53f45d9fc9SGennady Sharapov long ret, offset;
54f45d9fc9SGennady Sharapov unsigned long * data;
55f45d9fc9SGennady Sharapov unsigned long * syscall;
5616dd07bcSJeff Dike int err, pid = mm_idp->u.pid;
57f45d9fc9SGennady Sharapov
5816dd07bcSJeff Dike n = ptrace_setregs(pid, syscall_regs);
59b92c4f92SJeff Dike if (n < 0) {
60ba180fd4SJeff Dike printk(UM_KERN_ERR "Registers - \n");
61b92c4f92SJeff Dike for (i = 0; i < MAX_REG_NR; i++)
62ba180fd4SJeff Dike printk(UM_KERN_ERR "\t%d\t0x%lx\n", i, syscall_regs[i]);
63*d119595fSBenjamin Berg panic("%s : PTRACE_SETREGS failed, errno = %d\n",
64*d119595fSBenjamin Berg __func__, -n);
65b92c4f92SJeff Dike }
66f45d9fc9SGennady Sharapov
6716dd07bcSJeff Dike err = ptrace(PTRACE_CONT, pid, 0, 0);
6816dd07bcSJeff Dike if (err)
6916dd07bcSJeff Dike panic("Failed to continue stub, pid = %d, errno = %d\n", pid,
7016dd07bcSJeff Dike errno);
7116dd07bcSJeff Dike
7216dd07bcSJeff Dike wait_stub_done(pid);
73f45d9fc9SGennady Sharapov
74ba180fd4SJeff Dike /*
75ba180fd4SJeff Dike * When the stub stops, we find the following values on the
76f45d9fc9SGennady Sharapov * beginning of the stack:
77f45d9fc9SGennady Sharapov * (long )return_value
78f45d9fc9SGennady Sharapov * (long )offset to failed sycall-data (0, if no error)
79f45d9fc9SGennady Sharapov */
80f45d9fc9SGennady Sharapov ret = *((unsigned long *) mm_idp->stack);
81f45d9fc9SGennady Sharapov offset = *((unsigned long *) mm_idp->stack + 1);
82f45d9fc9SGennady Sharapov if (offset) {
8354ae36f2SJeff Dike data = (unsigned long *)(mm_idp->stack + offset - STUB_DATA);
84*d119595fSBenjamin Berg printk(UM_KERN_ERR "%s : ret = %ld, offset = %ld, data = %p\n",
85*d119595fSBenjamin Berg __func__, ret, offset, data);
86f45d9fc9SGennady Sharapov syscall = (unsigned long *)((unsigned long)data + data[0]);
87*d119595fSBenjamin Berg printk(UM_KERN_ERR "%s: syscall %ld failed, return value = 0x%lx, expected return value = 0x%lx\n",
88*d119595fSBenjamin Berg __func__, syscall[0], ret, syscall[7]);
89*d119595fSBenjamin Berg printk(UM_KERN_ERR " syscall parameters: 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
90f45d9fc9SGennady Sharapov syscall[1], syscall[2], syscall[3],
91f45d9fc9SGennady Sharapov syscall[4], syscall[5], syscall[6]);
92f45d9fc9SGennady Sharapov for (n = 1; n < data[0]/sizeof(long); n++) {
93f45d9fc9SGennady Sharapov if (n == 1)
94*d119595fSBenjamin Berg printk(UM_KERN_ERR " additional syscall data:");
95f45d9fc9SGennady Sharapov if (n % 4 == 1)
96ba180fd4SJeff Dike printk("\n" UM_KERN_ERR " ");
97f45d9fc9SGennady Sharapov printk(" 0x%lx", data[n]);
98f45d9fc9SGennady Sharapov }
99f45d9fc9SGennady Sharapov if (n > 1)
100f45d9fc9SGennady Sharapov printk("\n");
101f45d9fc9SGennady Sharapov }
102f45d9fc9SGennady Sharapov else ret = 0;
103f45d9fc9SGennady Sharapov
104f45d9fc9SGennady Sharapov *addr = check_init_stack(mm_idp, NULL);
105f45d9fc9SGennady Sharapov
106f45d9fc9SGennady Sharapov return ret;
107f45d9fc9SGennady Sharapov }
108f45d9fc9SGennady Sharapov
run_syscall_stub(struct mm_id * mm_idp,int syscall,unsigned long * args,long expected,void ** addr,int done)109f45d9fc9SGennady Sharapov long run_syscall_stub(struct mm_id * mm_idp, int syscall,
110f45d9fc9SGennady Sharapov unsigned long *args, long expected, void **addr,
111f45d9fc9SGennady Sharapov int done)
112f45d9fc9SGennady Sharapov {
113f45d9fc9SGennady Sharapov unsigned long *stack = check_init_stack(mm_idp, *addr);
114f45d9fc9SGennady Sharapov
115f45d9fc9SGennady Sharapov *stack += sizeof(long);
116f45d9fc9SGennady Sharapov stack += *stack / sizeof(long);
117f45d9fc9SGennady Sharapov
118f45d9fc9SGennady Sharapov *stack++ = syscall;
119f45d9fc9SGennady Sharapov *stack++ = args[0];
120f45d9fc9SGennady Sharapov *stack++ = args[1];
121f45d9fc9SGennady Sharapov *stack++ = args[2];
122f45d9fc9SGennady Sharapov *stack++ = args[3];
123f45d9fc9SGennady Sharapov *stack++ = args[4];
124f45d9fc9SGennady Sharapov *stack++ = args[5];
125f45d9fc9SGennady Sharapov *stack++ = expected;
126f45d9fc9SGennady Sharapov *stack = 0;
127f45d9fc9SGennady Sharapov
128c539ab73SJeff Dike if (!done && ((((unsigned long) stack) & ~UM_KERN_PAGE_MASK) <
129c539ab73SJeff Dike UM_KERN_PAGE_SIZE - 10 * sizeof(long))) {
130f45d9fc9SGennady Sharapov *addr = stack;
131f45d9fc9SGennady Sharapov return 0;
132f45d9fc9SGennady Sharapov }
133f45d9fc9SGennady Sharapov
134f45d9fc9SGennady Sharapov return do_syscall_stub(mm_idp, addr);
135f45d9fc9SGennady Sharapov }
136f45d9fc9SGennady Sharapov
syscall_stub_data(struct mm_id * mm_idp,unsigned long * data,int data_count,void ** addr,void ** stub_addr)137f45d9fc9SGennady Sharapov long syscall_stub_data(struct mm_id * mm_idp,
138f45d9fc9SGennady Sharapov unsigned long *data, int data_count,
139f45d9fc9SGennady Sharapov void **addr, void **stub_addr)
140f45d9fc9SGennady Sharapov {
141f45d9fc9SGennady Sharapov unsigned long *stack;
142f45d9fc9SGennady Sharapov int ret = 0;
143f45d9fc9SGennady Sharapov
144ba180fd4SJeff Dike /*
145ba180fd4SJeff Dike * If *addr still is uninitialized, it *must* contain NULL.
146f45d9fc9SGennady Sharapov * Thus in this case do_syscall_stub correctly won't be called.
147f45d9fc9SGennady Sharapov */
148c539ab73SJeff Dike if ((((unsigned long) *addr) & ~UM_KERN_PAGE_MASK) >=
149c539ab73SJeff Dike UM_KERN_PAGE_SIZE - (10 + data_count) * sizeof(long)) {
150f45d9fc9SGennady Sharapov ret = do_syscall_stub(mm_idp, addr);
151f45d9fc9SGennady Sharapov /* in case of error, don't overwrite data on stack */
152f45d9fc9SGennady Sharapov if (ret)
153f45d9fc9SGennady Sharapov return ret;
154f45d9fc9SGennady Sharapov }
155f45d9fc9SGennady Sharapov
156f45d9fc9SGennady Sharapov stack = check_init_stack(mm_idp, *addr);
157f45d9fc9SGennady Sharapov *addr = stack;
158f45d9fc9SGennady Sharapov
159f45d9fc9SGennady Sharapov *stack = data_count * sizeof(long);
160f45d9fc9SGennady Sharapov
161f45d9fc9SGennady Sharapov memcpy(stack + 1, data, data_count * sizeof(long));
162f45d9fc9SGennady Sharapov
163c539ab73SJeff Dike *stub_addr = (void *)(((unsigned long)(stack + 1) &
16454ae36f2SJeff Dike ~UM_KERN_PAGE_MASK) + STUB_DATA);
165f45d9fc9SGennady Sharapov
166f45d9fc9SGennady Sharapov return 0;
167f45d9fc9SGennady Sharapov }
168f45d9fc9SGennady Sharapov
map(struct mm_id * mm_idp,unsigned long virt,unsigned long len,int prot,int phys_fd,unsigned long long offset,int done,void ** data)16916dd07bcSJeff Dike int map(struct mm_id * mm_idp, unsigned long virt, unsigned long len, int prot,
17016dd07bcSJeff Dike int phys_fd, unsigned long long offset, int done, void **data)
171f45d9fc9SGennady Sharapov {
17216dd07bcSJeff Dike int ret;
173f45d9fc9SGennady Sharapov unsigned long args[] = { virt, len, prot,
174f45d9fc9SGennady Sharapov MAP_SHARED | MAP_FIXED, phys_fd,
175f45d9fc9SGennady Sharapov MMAP_OFFSET(offset) };
176f45d9fc9SGennady Sharapov
177f45d9fc9SGennady Sharapov ret = run_syscall_stub(mm_idp, STUB_MMAP_NR, args, virt,
178f45d9fc9SGennady Sharapov data, done);
179f45d9fc9SGennady Sharapov
180f45d9fc9SGennady Sharapov return ret;
181f45d9fc9SGennady Sharapov }
182f45d9fc9SGennady Sharapov
unmap(struct mm_id * mm_idp,unsigned long addr,unsigned long len,int done,void ** data)18364f60841SJeff Dike int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
18464f60841SJeff Dike int done, void **data)
185f45d9fc9SGennady Sharapov {
186f45d9fc9SGennady Sharapov int ret;
187f45d9fc9SGennady Sharapov unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0,
188f45d9fc9SGennady Sharapov 0 };
189f45d9fc9SGennady Sharapov
190f45d9fc9SGennady Sharapov ret = run_syscall_stub(mm_idp, __NR_munmap, args, 0,
191f45d9fc9SGennady Sharapov data, done);
192f45d9fc9SGennady Sharapov
193f45d9fc9SGennady Sharapov return ret;
194f45d9fc9SGennady Sharapov }
195f45d9fc9SGennady Sharapov
protect(struct mm_id * mm_idp,unsigned long addr,unsigned long len,unsigned int prot,int done,void ** data)196f45d9fc9SGennady Sharapov int protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
19716dd07bcSJeff Dike unsigned int prot, int done, void **data)
198f45d9fc9SGennady Sharapov {
19916dd07bcSJeff Dike int ret;
200f45d9fc9SGennady Sharapov unsigned long args[] = { addr, len, prot, 0, 0, 0 };
201f45d9fc9SGennady Sharapov
202f45d9fc9SGennady Sharapov ret = run_syscall_stub(mm_idp, __NR_mprotect, args, 0,
203f45d9fc9SGennady Sharapov data, done);
204f45d9fc9SGennady Sharapov
205f45d9fc9SGennady Sharapov return ret;
206f45d9fc9SGennady Sharapov }
207