Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #ifndef __SYSDEP_STUB_H |
| 7 | #define __SYSDEP_STUB_H |
| 8 | |
Jeff Dike | 5b7b15af | 2005-12-18 17:50:39 +0100 | [diff] [blame] | 9 | #include <sys/mman.h> |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 10 | #include <asm/unistd.h> |
| 11 | #include <sysdep/ptrace_user.h> |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame^] | 12 | #include "as-layout.h" |
Jeff Dike | 5b7b15af | 2005-12-18 17:50:39 +0100 | [diff] [blame] | 13 | #include "stub-data.h" |
| 14 | #include "kern_constants.h" |
| 15 | #include "uml-config.h" |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 16 | |
| 17 | extern void stub_segv_handler(int sig); |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 18 | extern void stub_clone_handler(void); |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 19 | |
| 20 | #define STUB_SYSCALL_RET PT_INDEX(RAX) |
| 21 | #define STUB_MMAP_NR __NR_mmap |
| 22 | #define MMAP_OFFSET(o) (o) |
| 23 | |
Paolo 'Blaisorblade' Giarrusso | 4f02724 | 2005-11-07 00:58:46 -0800 | [diff] [blame] | 24 | #define __syscall_clobber "r11","rcx","memory" |
| 25 | #define __syscall "syscall" |
| 26 | |
Jeff Dike | 17d4697 | 2005-11-21 21:32:09 -0800 | [diff] [blame] | 27 | static inline long stub_syscall0(long syscall) |
| 28 | { |
| 29 | long ret; |
| 30 | |
| 31 | __asm__ volatile (__syscall |
| 32 | : "=a" (ret) |
| 33 | : "0" (syscall) : __syscall_clobber ); |
| 34 | |
| 35 | return ret; |
| 36 | } |
| 37 | |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 38 | static inline long stub_syscall2(long syscall, long arg1, long arg2) |
| 39 | { |
| 40 | long ret; |
| 41 | |
Paolo 'Blaisorblade' Giarrusso | 4f02724 | 2005-11-07 00:58:46 -0800 | [diff] [blame] | 42 | __asm__ volatile (__syscall |
| 43 | : "=a" (ret) |
| 44 | : "0" (syscall), "D" (arg1), "S" (arg2) : __syscall_clobber ); |
| 45 | |
| 46 | return ret; |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static inline long stub_syscall3(long syscall, long arg1, long arg2, long arg3) |
| 50 | { |
Paolo 'Blaisorblade' Giarrusso | 4f02724 | 2005-11-07 00:58:46 -0800 | [diff] [blame] | 51 | long ret; |
| 52 | |
| 53 | __asm__ volatile (__syscall |
| 54 | : "=a" (ret) |
| 55 | : "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3) |
| 56 | : __syscall_clobber ); |
| 57 | |
| 58 | return ret; |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static inline long stub_syscall4(long syscall, long arg1, long arg2, long arg3, |
| 62 | long arg4) |
| 63 | { |
Paolo 'Blaisorblade' Giarrusso | 4f02724 | 2005-11-07 00:58:46 -0800 | [diff] [blame] | 64 | long ret; |
| 65 | |
| 66 | __asm__ volatile ("movq %5,%%r10 ; " __syscall |
| 67 | : "=a" (ret) |
| 68 | : "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3), |
| 69 | "g" (arg4) |
| 70 | : __syscall_clobber, "r10" ); |
| 71 | |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | static inline long stub_syscall5(long syscall, long arg1, long arg2, long arg3, |
| 76 | long arg4, long arg5) |
| 77 | { |
| 78 | long ret; |
| 79 | |
| 80 | __asm__ volatile ("movq %5,%%r10 ; movq %6,%%r8 ; " __syscall |
| 81 | : "=a" (ret) |
| 82 | : "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3), |
| 83 | "g" (arg4), "g" (arg5) |
| 84 | : __syscall_clobber, "r10", "r8" ); |
| 85 | |
| 86 | return ret; |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 89 | static inline void trap_myself(void) |
| 90 | { |
| 91 | __asm("int3"); |
| 92 | } |
| 93 | |
Jeff Dike | 5b7b15af | 2005-12-18 17:50:39 +0100 | [diff] [blame] | 94 | static inline void remap_stack(long fd, unsigned long offset) |
| 95 | { |
| 96 | __asm__ volatile ("movq %4,%%r10 ; movq %5,%%r8 ; " |
| 97 | "movq %6, %%r9; " __syscall "; movq %7, %%rbx ; " |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame^] | 98 | "movq %%rax, (%%rbx)": |
| 99 | : "a" (STUB_MMAP_NR), "D" (STUB_DATA), |
| 100 | "S" (UM_KERN_PAGE_SIZE), |
| 101 | "d" (PROT_READ | PROT_WRITE), |
| 102 | "g" (MAP_FIXED | MAP_SHARED), "g" (fd), |
Jeff Dike | 5b7b15af | 2005-12-18 17:50:39 +0100 | [diff] [blame] | 103 | "g" (offset), |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame^] | 104 | "i" (&((struct stub_data *) STUB_DATA)->err) |
Jeff Dike | 5b7b15af | 2005-12-18 17:50:39 +0100 | [diff] [blame] | 105 | : __syscall_clobber, "r10", "r8", "r9" ); |
| 106 | } |
| 107 | |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 108 | #endif |