Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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