LCOV - code coverage report
Current view: top level - arch/um/os-Linux/skas - mem.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 7 84 8.3 %
Date: 2023-04-06 08:38:28 Functions: 1 7 14.3 %

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

Generated by: LCOV version 1.14