Rename VM_PROT_* flags to VM_ACCESS_*

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Rename FILE_RIGHT_* flags to FILE_ACCESS_*

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Minor style cleanup

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
User mode thread interrupt system

The user mode interrupt system provides a mechanism to interrupt a
thread's execution and execute a user mode handler function. When the
handler returns the thread's state will be restored and it will resume
where it was previously executing. Interrupts can have an associated
data block which is copied onto the user stack, and the handler is
passed a pointer to it (as well as a pointer to the saved thread
state).

Interrupts have priorities, and threads have an interrupt priority
level (IPL) which can be used to block interrupts based on their
priority. If an interrupt is queued which is blocked by the IPL, it
will be executed once the IPL is lowered to unblock the interrupt.

There are 2 planned users of the interrupt system: thread exception
handling, and asynchronous object event notifications (it will be
possible to set up an asynchronous wait for an object event and have
a callback called when the event is triggered).

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
User mode exception handling API

Exception handling is built on top of the user mode interrupt system,
handler functions can be registered to handle exceptions. There are
process-wide and per-thread handler tables. If a per-thread handler is
set it will be used, otherwise the per-process handler is used. If no
handler is set either in either table, the process will be killed.

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Deliver user mode exceptions for unhandled page faults

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
AMD64: Clean up interrupt handling, handle user-mode exceptions

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Add EXCEPTION_STACK_OVERFLOW for when a thread hits its stack guard page

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Add an exit reason to threads as well as processes

This allows a process to determine whether one of its threads was killed
instead of exiting normally. We use the same EXIT_REASON_* definitions
as for processes, which are moved out into kernel/exit.h. Also add a
privilege check for getting process/thread exit status.

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Add kern_{process,thread}_kill() system calls

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Thread main functions now have a return value, simplify kern_thread_create()

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Inherit exception handler tables in kern_process_clone()

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Fix definition of kern_thread_create() in syscalls.def

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Fix handle leak if thread TLS initialization fails

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Add kern_thread_raise() syscall to raise an exception

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Rename thread_state_t to thread_context_t

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Allow threads to specify an alternate stack to execute exception handlers on

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Re-add temporary KDB entry on user exceptions

Since there are no user debugging facilities yet, it is useful to enter
KDB on user exceptions to debug them.

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Clean up libkernel

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>
Refactor TLS initialization

We now allocate TLS blocks before creating a new thread and then just
set the TLS address once the new thread is started. This avoids having
to handle failure in the thread entry trampoline. Also remove the
separate TLS module IDs and use the image ID instead.

Signed-off-by: Alex Smith <alex@alex-smith.me.uk>