/* 
 * Copyright 2010 Stefan Lankes, Chair for Operating Systems,
 *                               RWTH Aachen University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Standard x86 syscalls for user programs running under MetalSVM
 */

#ifndef __SYSCALL_H__
#define __SYSCALL_H__

#ifdef __cplusplus
extern "C" {
#endif

#define __NR_exit 		0
#define __NR_write		1
#define __NR_open		2
#define __NR_close		3
#define __NR_read		4
#define __NR_lseek		6
#define __NR_unlink		7
#define __NR_getpid		8
#define __NR_kill		9
#define __NR_fstat		10
#define __NR_sbrk		11
#define __NR_fork		12
#define __NR_wait		13
#define __NR_execve		14
#define __NR_times		15

#define _STR(token)             #token
#define _SYSCALLSTR(x)          "int $" _STR(x) " "
#define INT_SYSCALL		0x80

inline static long
syscall(int nr, unsigned long arg0, unsigned long arg1, unsigned long arg2,
	unsigned long arg3, unsigned long arg4)
{
	long res;

	asm volatile (_SYSCALLSTR(INT_SYSCALL)
	     : "=a" (res)
	     : "0" (nr), "b" (arg0), "c" (arg1), "d" (arg2), "S" (arg3), "D" (arg4)
	     : "memory", "cc");

	return res;
}

#define SYSCALL0(NR) \
	syscall(NR, 0, 0, 0, 0, 0)
#define SYSCALL1(NR, ARG1) \
	syscall(NR, (unsigned long)ARG1, 0, 0, 0, 0)
#define SYSCALL2(NR, ARG1, ARG2) \
	syscall(NR, (unsigned long)ARG1, (unsigned long)ARG2, 0, 0, 0)
#define SYSCALL3(NR, ARG1, ARG2, ARG3) \
	syscall(NR, (unsigned long)ARG1, (unsigned long)ARG2, (unsigned long)ARG3, 0, 0)
#define SYSCALL4(NR, ARG1, ARG2, ARG3, ARG4) \
	syscall(NR, (unsigned long)ARG1, (unsigned long)ARG2, (unsigned long)ARG3, (unsigned long) ARG4, 0)
#define SYSCALL5(NR, ARG1, ARG2, ARG3, ARG4) \
	syscall(NR, (unsigned long)ARG1, (unsigned long)ARG2, (unsigned long)ARG3, (unsigned long) ARG4, (unsigned long) ARG5)

#ifdef __cplusplus
}
#endif

#endif