/* arch/posix.h - posix constants
 *  ? this file defines posix magic numbers
 *    needed in syscalls, both cross-platform
 *    ones and os-dependent ones. note that
 *    the values may change depending on the
 *    OS specified! */

#ifndef KIplatform_posix
#define KIplatform_posix
#include <k/def.h>
#include <k/type.h>

#if (!defined(KFplatform_define_constants)) && \
    (!defined(KFplatform_define_types)) && \
    (!defined(KFplatform_define_funcs))
#define KFplatform_define_constants
#define KFplatform_define_types
#define KFplatform_define_funcs
#endif

#ifdef KFplatform_define_constants

enum posix_prot {
	posix_prot_none  = 0,
	posix_prot_read  = 1 << 0,
	posix_prot_write = 1 << 1,
	posix_prot_exec  = 1 << 2
};

enum posix_map {
	posix_map_shared  = 1,
	posix_map_private = 2
};

enum posix_flag {
	posix_flag_fixed     = 0x10,
#if KVos == KA_os_lin
	posix_flag_anonymous = 0x20,
#elif KVos == KA_os_fbsd
	posix_flag_anonymous = 0x1000,
#endif

	/* platform flags */
	posix_flag_linux_hugetlb = 0x40000
};

enum posix_signal {
	/* common signal numbers across arches and OSes */
	posix_signal_hangup = 1,
	posix_signal_interrupt = 2,
	posix_signal_quit = 3,
	posix_signal_illegal = 4,
	posix_signal_trap = 5,
	posix_signal_abort = 6,
	posix_signal_float = 8,
	posix_signal_kill = 9,
	posix_signal_segfault = 11,
	posix_signal_pipe = 13,
	posix_signal_alarm = 14,
	posix_signal_terminate = 15,

#	if KVarch == KA_arch_mips && KVos == KA_os_lin
	/* for some ungodly reason, some signal
	 * numbers on MIPS differ from most other
	 * architectures still in contemporary use */
		posix_signal_bus = 10,
		posix_signal_user_a = 16,
		posix_signal_user_b = 17,
		posix_signal_child = 18,
		posix_signal_continue = 25,
		posix_signal_stop = 23,
		posix_signal_terminal_stop = 24,
		posix_signal_tty_input = 26,
		posix_signal_tty_output = 27,
		posix_signal_io_urgent = 21,
		posix_signal_limit_cpu = 30,
		posix_signal_limit_space = 31,
		posix_signal_vt_alarm = 28,
		posix_signal_profile = 29,
		posix_signal_winch = 20,
		posix_signal_poll = 22,
		posix_signal_power = 19,
#	elif KVos == KA_os_lin
	/* x86, ARM, and most others use these
	 * signal numbers */
		posix_signal_bus = 7,
		posix_signal_user_a = 10,
		posix_signal_user_b = 12,
		posix_signal_child = 17,
		posix_signal_continue = 18,
		posix_signal_stack_fault = 16,
		posix_signal_stop = 19,
		posix_signal_terminal_stop = 20,
		posix_signal_tty_input = 21,
		posix_signal_tty_output = 22,
		posix_signal_io_urgent = 23,
		posix_signal_limit_cpu = 24,
		posix_signal_limit_space = 25,
		posix_signal_vt_alarm = 26,
		posix_signal_profile = 27,
		posix_signal_winch = 28,
		posix_signal_poll = 29,
		posix_signal_power = 30,
#	elif KVos == KA_os_fbsd
		posix_signal_bus = 10,
		posix_signal_user_a = 30,
		posix_signal_user_b = 31,
		posix_signal_child = 17,
		posix_signal_continue = 19,
		posix_signal_stack_fault = 16,
		posix_signal_stop = 17,
		posix_signal_terminal_stop = 18,
		posix_signal_tty_input = 21,
		posix_signal_tty_output = 22,
		posix_signal_io_urgent = 16,
		posix_signal_limit_cpu = 24,
		posix_signal_limit_space = 25,
		posix_signal_vt_alarm = 26,
		posix_signal_profile = 27,
		posix_signal_winch = 28,
		posix_signal_info = 29,
		posix_signal_poll = 23,
		posix_signal_power = 30,
#	endif
};

#endif
#ifdef KFplatform_define_types

/* platform types */

typedef s64 k_platform_syscall_return;
typedef u64 k_platform_syscall_error;

#if KVos == KA_os_lin
	typedef long k_platform_syscall_arg;
#elif KVos == KA_os_fbsd
	typedef u64  k_platform_syscall_arg;
#else
	/* we're going to just pick a sane
	 * fallback that's reasonably likely
	 * to work with most systems one way
	 * or another */
	typedef unsigned long long k_platform_syscall_arg;
#endif

struct k_platform_syscall_answer {
	k_platform_syscall_return ret;
	k_platform_syscall_error error;
};

#endif

#if defined(KFplatform_define_constants) ||\
    defined(KFplatform_define_funcs)
	/* the specific system call table to use is
	 * selected by the build script; /arch/$target
	 * is set as an include directory */
#	include <system_calls.h>
#endif

#ifdef KFplatform_define_funcs

volatile extern struct k_platform_syscall_answer
k_platform_syscall(volatile enum k_platform_syscall call,
		volatile u8 valency, volatile k_platform_syscall_arg args[]);

#endif

#endif
