stub.h 2.3 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com)
 * Licensed under the GPL
 */

#ifndef __SYSDEP_STUB_H
#define __SYSDEP_STUB_H

J
Jeff Dike 已提交
9
#include <sys/mman.h>
10 11
#include <asm/unistd.h>
#include <sysdep/ptrace_user.h>
J
Jeff Dike 已提交
12
#include "as-layout.h"
J
Jeff Dike 已提交
13 14
#include "stub-data.h"
#include "kern_constants.h"
15 16

extern void stub_segv_handler(int sig);
17
extern void stub_clone_handler(void);
18 19 20 21 22

#define STUB_SYSCALL_RET PT_INDEX(RAX)
#define STUB_MMAP_NR __NR_mmap
#define MMAP_OFFSET(o) (o)

23 24 25
#define __syscall_clobber "r11","rcx","memory"
#define __syscall "syscall"

26 27 28 29 30 31 32 33 34 35 36
static inline long stub_syscall0(long syscall)
{
	long ret;

	__asm__ volatile (__syscall
		: "=a" (ret)
		: "0" (syscall) : __syscall_clobber );

	return ret;
}

37 38 39 40
static inline long stub_syscall2(long syscall, long arg1, long arg2)
{
	long ret;

41 42 43 44 45
	__asm__ volatile (__syscall
		: "=a" (ret)
		: "0" (syscall), "D" (arg1), "S" (arg2) : __syscall_clobber );

	return ret;
46 47 48 49
}

static inline long stub_syscall3(long syscall, long arg1, long arg2, long arg3)
{
50 51 52 53 54 55 56 57
	long ret;

	__asm__ volatile (__syscall
		: "=a" (ret)
		: "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3)
		: __syscall_clobber );

	return ret;
58 59 60 61 62
}

static inline long stub_syscall4(long syscall, long arg1, long arg2, long arg3,
				 long arg4)
{
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	long ret;

	__asm__ volatile ("movq %5,%%r10 ; " __syscall
		: "=a" (ret)
		: "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3),
		  "g" (arg4)
		: __syscall_clobber, "r10" );

	return ret;
}

static inline long stub_syscall5(long syscall, long arg1, long arg2, long arg3,
				 long arg4, long arg5)
{
	long ret;

	__asm__ volatile ("movq %5,%%r10 ; movq %6,%%r8 ; " __syscall
		: "=a" (ret)
		: "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3),
		  "g" (arg4), "g" (arg5)
		: __syscall_clobber, "r10", "r8" );

	return ret;
86 87
}

J
Jeff Dike 已提交
88
static inline void trap_myself(void)
89
{
J
Jeff Dike 已提交
90
	__asm("int3");
91 92
}

J
Jeff Dike 已提交
93
static inline void remap_stack(long fd, unsigned long offset)
94
{
J
Jeff Dike 已提交
95 96
	__asm__ volatile ("movq %4,%%r10 ; movq %5,%%r8 ; "
			  "movq %6, %%r9; " __syscall "; movq %7, %%rbx ; "
J
Jeff Dike 已提交
97 98 99 100 101
			  "movq %%rax, (%%rbx)":
			  : "a" (STUB_MMAP_NR), "D" (STUB_DATA),
			    "S" (UM_KERN_PAGE_SIZE),
			    "d" (PROT_READ | PROT_WRITE),
                            "g" (MAP_FIXED | MAP_SHARED), "g" (fd),
J
Jeff Dike 已提交
102
			    "g" (offset),
J
Jeff Dike 已提交
103
			    "i" (&((struct stub_data *) STUB_DATA)->err)
J
Jeff Dike 已提交
104
			  : __syscall_clobber, "r10", "r8", "r9" );
105 106
}

107
#endif