162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com)
362306a36Sopenharmony_ci * Licensed under the GPL
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef __SYSDEP_STUB_H
762306a36Sopenharmony_ci#define __SYSDEP_STUB_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <sysdep/ptrace_user.h>
1062306a36Sopenharmony_ci#include <generated/asm-offsets.h>
1162306a36Sopenharmony_ci#include <linux/stddef.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#define STUB_MMAP_NR __NR_mmap
1462306a36Sopenharmony_ci#define MMAP_OFFSET(o) (o)
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#define __syscall_clobber "r11","rcx","memory"
1762306a36Sopenharmony_ci#define __syscall "syscall"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistatic inline long stub_syscall0(long syscall)
2062306a36Sopenharmony_ci{
2162306a36Sopenharmony_ci	long ret;
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci	__asm__ volatile (__syscall
2462306a36Sopenharmony_ci		: "=a" (ret)
2562306a36Sopenharmony_ci		: "0" (syscall) : __syscall_clobber );
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	return ret;
2862306a36Sopenharmony_ci}
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cistatic inline long stub_syscall2(long syscall, long arg1, long arg2)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	long ret;
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	__asm__ volatile (__syscall
3562306a36Sopenharmony_ci		: "=a" (ret)
3662306a36Sopenharmony_ci		: "0" (syscall), "D" (arg1), "S" (arg2) : __syscall_clobber );
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	return ret;
3962306a36Sopenharmony_ci}
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic inline long stub_syscall3(long syscall, long arg1, long arg2, long arg3)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	long ret;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	__asm__ volatile (__syscall
4662306a36Sopenharmony_ci		: "=a" (ret)
4762306a36Sopenharmony_ci		: "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3)
4862306a36Sopenharmony_ci		: __syscall_clobber );
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	return ret;
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic inline long stub_syscall4(long syscall, long arg1, long arg2, long arg3,
5462306a36Sopenharmony_ci				 long arg4)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	long ret;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	__asm__ volatile ("movq %5,%%r10 ; " __syscall
5962306a36Sopenharmony_ci		: "=a" (ret)
6062306a36Sopenharmony_ci		: "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3),
6162306a36Sopenharmony_ci		  "g" (arg4)
6262306a36Sopenharmony_ci		: __syscall_clobber, "r10" );
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	return ret;
6562306a36Sopenharmony_ci}
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistatic inline long stub_syscall5(long syscall, long arg1, long arg2, long arg3,
6862306a36Sopenharmony_ci				 long arg4, long arg5)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	long ret;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	__asm__ volatile ("movq %5,%%r10 ; movq %6,%%r8 ; " __syscall
7362306a36Sopenharmony_ci		: "=a" (ret)
7462306a36Sopenharmony_ci		: "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3),
7562306a36Sopenharmony_ci		  "g" (arg4), "g" (arg5)
7662306a36Sopenharmony_ci		: __syscall_clobber, "r10", "r8" );
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	return ret;
7962306a36Sopenharmony_ci}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_cistatic inline void trap_myself(void)
8262306a36Sopenharmony_ci{
8362306a36Sopenharmony_ci	__asm("int3");
8462306a36Sopenharmony_ci}
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cistatic inline void remap_stack_and_trap(void)
8762306a36Sopenharmony_ci{
8862306a36Sopenharmony_ci	__asm__ volatile (
8962306a36Sopenharmony_ci		"movq %0,%%rax ;"
9062306a36Sopenharmony_ci		"movq %%rsp,%%rdi ;"
9162306a36Sopenharmony_ci		"andq %1,%%rdi ;"
9262306a36Sopenharmony_ci		"movq %2,%%r10 ;"
9362306a36Sopenharmony_ci		"movq %%rdi,%%r8 ; addq %3,%%r8 ; movq (%%r8),%%r8 ;"
9462306a36Sopenharmony_ci		"movq %%rdi,%%r9 ; addq %4,%%r9 ; movq (%%r9),%%r9 ;"
9562306a36Sopenharmony_ci		__syscall ";"
9662306a36Sopenharmony_ci		"movq %%rsp,%%rdi ; andq %1,%%rdi ;"
9762306a36Sopenharmony_ci		"addq %5,%%rdi ; movq %%rax, (%%rdi) ;"
9862306a36Sopenharmony_ci		"int3"
9962306a36Sopenharmony_ci		: :
10062306a36Sopenharmony_ci		"g" (STUB_MMAP_NR),
10162306a36Sopenharmony_ci		"g" (~(STUB_DATA_PAGES * UM_KERN_PAGE_SIZE - 1)),
10262306a36Sopenharmony_ci		"g" (MAP_FIXED | MAP_SHARED),
10362306a36Sopenharmony_ci		"g" (UML_STUB_FIELD_FD),
10462306a36Sopenharmony_ci		"g" (UML_STUB_FIELD_OFFSET),
10562306a36Sopenharmony_ci		"g" (UML_STUB_FIELD_CHILD_ERR),
10662306a36Sopenharmony_ci		"S" (STUB_DATA_PAGES * UM_KERN_PAGE_SIZE),
10762306a36Sopenharmony_ci		"d" (PROT_READ | PROT_WRITE)
10862306a36Sopenharmony_ci		:
10962306a36Sopenharmony_ci		__syscall_clobber, "r10", "r8", "r9");
11062306a36Sopenharmony_ci}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_cistatic __always_inline void *get_stub_data(void)
11362306a36Sopenharmony_ci{
11462306a36Sopenharmony_ci	unsigned long ret;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	asm volatile (
11762306a36Sopenharmony_ci		"movq %%rsp,%0 ;"
11862306a36Sopenharmony_ci		"andq %1,%0"
11962306a36Sopenharmony_ci		: "=a" (ret)
12062306a36Sopenharmony_ci		: "g" (~(STUB_DATA_PAGES * UM_KERN_PAGE_SIZE - 1)));
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	return (void *)ret;
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci#endif
125