KAPI_CALL implemented; fs,socket,httpd use it

DOC/CallingConvention.txt:
	document attempts to use #NP and #GP.

kernel/gdt.s:
	Remove GDT_ring0SS.
	Add GDT_tss_np (segment not present).

	Increase kernel_tss0_stack_top due to having more TSS.
	[TODO: use alloc_task_priv_stack]

	declare GDT_kapi with ACC_DC|ACC_RING0 so RING3 can call it.

kernel/idt.s:
	Debug updates (temp):
		double fault: only print 'Double Fault' and then halt.
		page fault: add debug.
		add segment not present / general protection fault task
		handlers.

	add idt_init_ex method: set up the tss, handler, and idt for
	the given exception.

	debug_print_gdt_descriptor: print base/limit. Fix ACC_SYS->NRM flag.

kernel/kapi.s:
	add KAPI_CALL macro.
	update kapi_pf: chain to next if not KAPI call.
	add KAPI_PF_DEBUG constant and update debug printing.
	do the actual call.
	add kapi_np handler (unused) and some debug.
	update kapi_pf (unused): print selector flags/access, tss link.
	mov cmd_kapi down.
	cmd_kapi_test: call fs_openfile as test.
	kapi_pf: preserve our & task flags.

kernel/paging.s:
	paging_init: set CR3 on TSS_NP.

kernel/shell.s:
	update cmd_cat$: use KAPI_CALL fs_openfile.

kernel/tss.s:
	declare TSS_NP.

kernel/fs.s:
	add KAPI_DECLARE for core methods.
	return filesize in ecx for fs_stat, fs_open.

kernel/httpd.s:
	use KAPI_CALL for all socket and fs functions.
	Use the new fs_read (we provide the buffer).

kernel/socket.s:
	add KAPI_DECLARE for core methods.
	Remove CPL0 testing and entering - all methods can (should) only be
	called from kernel mode.
Fix physical page allocation management.

kernel/mem.s:
	fix a few bugs that appeared when using more than 32 pages.
[minor] update tss printing.
KAPI: implement stack methods & CPL0->CPL0 calls.

kernel/defines.s:
	declare SECTION_DATA_KAPI_ARG to contain the nr of stack arguments.

kernel/gdt.s:
	update GDT_kernelGate: specify 2 dword params.

kernel/idt.s:
	print some more details on double fault.

kernel/kapi.s:
	declare SECTION_DATA_KAPI_ARG and update KAPI_DECLARE to store
	the nr of stackargs.
	kapi_init: update GDT_kernelGate: set the address to kapi_callgate.

	kapi_pf: change the proxying: if the call is made from CPL0,
	calling a callgate won't result in ss:esp being updated. In this
	case, update the suspended TSS's EIP and stack to simulate a near
	call. In the case of a privilege level change, use kapi_proxy.
	Further: store the number of stackargs on the user stack.

	kapi_proxy: simply call the kernelGate (pointing to kapi_callgate),
	and retf.

	kapi_callgate: handle stackargs. Only the arg count and method are
	passed on the privileged stack. If there are stackargs, copy them.

kernel/kernel.s:
	schedule the kcons with CPL1.

kernel/keyboard.s:
	call schedule_task using KAPI_CALL in the keyboard isr.

kernel/schedule.s:
	declare schedule_task as KAPI with 4 stackargs.
	remove code to enter CPL0 and update CR3 with the kernel PD.
	The method is now expected to only be called from kernel context.

kernel/shell.s:
	use KAPI_CALL schedule_task in fork rather than direct call.
KAPI: use INT32 gate

DOC/CallingConvention.txt:
	Documenting the INT32 gate approach.

kernel/kapi.s:
	use int32 gate rather than task32 gate: only one stackswitch done.
	Implement stackarg methods.
	Note: the stack is always adjusted (copied), so this is somewhat slow
	and hard to see clearly.
	TODO: optimize further, cleanup.

	Tested in CPL0 (stackarg only) and CPL1.
GDT callgates/IDT: static initialisation

	Found a way to do offset math in GAS: subtract .text to make
	the offset *ABS*. Then, it can be shifted, masked etc.

kernel/gdt.s:
	initialize the callgates statically, now that the method offset
	can be calculated with.

kernel/idt.s:
	initialize the IDT handler offsets to isr_jump_table statically.
	remove the initialisation loop.
GDT: minor updates.

kernel/gdt.s:
	add an LDT descriptor.
	declare the types (ACC_T_*) of descriptors.
	Update GDT_(GET|SET)_(FLAGS|ACCESS|BASE|LIMIT) with an optional table
	argument.
IDT: fix IDT code segment & minor updates.

kernel/idt.s:
	use SEL_compatCS rather than SEL_flatCS in the IDT.
	don't use the #PF and #NP task handlers.
Fix printing paging structure.

kernel/paging.s:
	fix a jump to a nonexisting label causing unexpected results.
Exception handler: print PTE value aswell
Schedule all tasks in ring2.

kernel/schedule.s:
	declare TASK_FLAG_RING_SERVICE alias for easy CPL2/CPL3 config.
	At current, CPL2: this means, that all tasks are considered privileged
	by paging (the PDE/PTE_FLAG_U applies to ring3 vs ring012).

	Affected:

	console.s
	kernel.s
	net/dns.s
	net/httpd.s
	net/net.s
	net/smtp.s
	net/ssh.s

kernel/kernel.s:
	TASK_FLAG_RING_SERVICE
	data_kapi_ldt end

kernel/console.s:
	TASK_FLAG_RING_SERVICE
	call schedule using KAPI_CALL.
KAPI: sysenter, callgates; fix task privstack.

	SUMMARY:
		Split kapi.s into separate files for each 'mode'.
		Add partial implementation for callgates, sysenter/exit,
		and int 0x80.
		Fix a bug in stackpage allocation status management, which
		caused the same privileged stack to be used for different tasks.

kernel/Makefile:
	add disabled AS cmdline for when KAPI_MODE_SYSENTER.
	update kapi kernel sources.

kernel/defines.s:
	define an LDT section for KAPI_MODE_*CALLGATE

kernel/gdt.s:
	swap declaration of GDT_compatSS and GDT_compatDS. [no real change].
	add 4 flat selectors for SYSENTER/SYSEXIT.

kernel/kapi.s:
	split the various methods into separate files and include them.
	Update the kapi_init method to call the appropriate kapi_init_* method.
	Set the KAPI_MODE to PAGE_INT.

kernel/kapi_int80.s:	[Add]
	implement int 0x80 kernel call; UNFINISHED.

kernel/kapi_page_callgate.s:
	add unfinished callgate method. The idea is that the paging api
	is used, and the far call's selector is replaced with an LDT callgate
	selector, so that stackarg handling and such will take care of itself.
	At current, it doesn't work.

kernel/kapi_page_int.s:
	similar to kapi_page_task, except that this uses an interrupt gate
	rather than a task gate, which is faster, as the elevated stack
	used will be the one set up by the scheduler for the task (the generic
	task switch TSS), rather than the stack from the paging-task-swich TSS.
	Compared with page_task, it does not use task-switches at all, but
	only a stack elevation.

kernel/kapi_page_task.s:
	content moved from kapi.s.
	This method works, but is slower than kapi_page_int. It remains
	part of the source tree, in case the scheduler will use hardware
	task switching at some point.

kernel/kapi_sysenter.s:
	implement SYSENTER/SYSEXIT. It works up to SYSEXIT. The main difficulty
	is that the selectors used must be flat (base 0, limit 4Gb), and that
	only RING0 and RING3 can be used, whereas the kernel and tasks at
	current have their base != 0 and their limit as small as possible.

kernel/kernel.s:
	don't require two enters to open shell.
	add 'kernel_idle$' debug symbol for shorter line in 'ps'.
	declare data_kapi_ldt_end.

kernel/pmode.s:
	declare kernel_sysenter_stack.

kernel/tss.s:
	initialize kernel_sysenter_stack (after the tss stacks).

kernel/schedule.s:
	fix 'top' - schedule_print expects ebp to point to the task.
	add 'idle_task$' debug symbol.

	Fix privileged stack allocation:
		- use 'btr' (reset) and not 'btc' (complement, not clear!).
		- add disabled assertion for duplicate priv stack allocation

	add note to task_wait_io: should be system call due to task structure
	access.

	update cmd_tasks:
		- don't print the current task at the top.
		- remove redundant code.
		- don't print the index in decimal (already printed in hex).
		- print the privileged stack top in the 'addr' field.
			This is left because httpd uses the wrong privileged
			stack! TODO FIX.
		- print the label+relative offset as 2 digits if possible.

DOC/CallingConvention.txt:
	some notes on callgates and sysenter/exit
Fix virtual consoles.

kernel/console.s:
	set eax to console nr instead of al, as console_kb_set
	takes eax as input. This accounted for the hangs.

kernel/keyboard.s:
	minor aesthetic update: don't call console_kb_get again
	in buf_err$ as it is already present in ebx.

kernel/print.s:
	console_set: call mallocz instead of malloc: fixes the
	random data sometimes seen on initial secondary console
	activation.
Fix task cleanup.

	SUMMARY:

		So far, a task issues a 'ret' when it's done, and enters
		into the scheduler's task cleanup code: marking the task
		as deleted, and freeing up the allocated pages.
		However, this is done in the task's context, which means
		it has access to the scheduler structures, which is undesirable.

		For now, the 'task_done' is a 'ret' catch that invokes the new
		task_exit KAPI which does not return - for backwards
		compatibility. This catch code resides in kernel space and
		will be moved shortly.

		The task's stack is now freed in the scheduler's ISR,
		instead of being freed while the task stack is active.

kernel/mem.s:
	check if page to be freed is not free already.

kernel/schedule.s:
	declare KAPI 'yield' (schedule_near).
	declare KAPI 'task_wait_io' - not called as yet.

	rework task_done: mark the task as TASK_FLAG_DONE, but do not free
	anything just yet.
	scheduler_get_task: call task_cleanup$ when encountering a DONE task.

	The next commit will contain a fix for calling stackarg KAPI methods
	from CPL != 0. Some tests to detect faulty calls are added in
	schedule_task: add disabled debug; check task flag validity.

	free_task_priv_stack: preserve registers (due to called from
	different location).
KAPI: fix CPL>0 stack usage

	SUMMARY:

		KAPI_MODE_PAGE_INT was untested for CPL>0 stackarg
		methods. Fixes two bugs: using the wrong stackarg
		size, and, overflowing stack usage into the next
		stackpage (which did not pose a problem).

kernel/kapi_page_int.s:
	use correct stackarg size.
	fix stack underflow.
Use yield system call.

kernel/mutex.s:
	make schedule_near a system call, called 'yield'.
ISO9660: filename copy limit fix

kernel/iso9660.s:
	ecx is used as upper limit to copy the filename, but it was not
	initialized properly.
Network services: unprivileged kernel calls

	SUMMARY:

		Use KAPI_CALLs in the network service code, and have
		all services run unprivileged.

		Change the NET_BUFFER_* macro's and methods slightly,
		to still allow for non-privileged calls, such as
		used by DNS and ICMP which don't always use sockets.

kernel/fs.s:
	KAPI declare fs_nextentry;
	remove unused fs_handle_read_.

kernel/net/dns.s:
kernel/net/httpd.s:
kernel/net/icmp.s:
kernel/net/sipd.s:
kernel/net/smtp.s:
	KAPI_CALLs for schedule_task and/or socket_*.

kernel/net/net.s:
	make net_buffer_allocate($) 'private' - for use by
	net_buffer_get only.
	change NET_BUFFER_GET macro to call the method directly.
	update net_buffer_get to return edi.

	make net_buffer_send a KAPI method.
	add net_buffer_send$: check CPL, and KAPI_CALL if needed.

	KAPI_CALL schedule_task.

kernel/net/socket.s:
	KAPI declare socket_get_lport (used by dns.s).
Shell: use KAPI calls for fs; minor comments

kernel/fs.s:
	fs_open: document ecx return value.
	remove fs_open_.

kernel/hwdebug.s:
	disable the check and print something.

kernel/mem.s:
	malloc_page_phys: add disabled allocated page check and
	add clarifying comment.

kernel/paging.s:
	paging_idmap_page_f: print more info on error.
	paging_alloc_page_idmap: fix in/out documentation.

kernel/shell.s:
	call fs cricital methods using the KAPI. (Now ls, cd etc
	work again).

	rework cmd_exe to read the data ourselves without the
	fs_handle_read$ hack.

	use YIELD instead of hlt in clock_task.
Task suspend/resume cmdline.

	SUMMARY:

		Fix 'kill'.
		Implement task suspend/resume in shell.
		Test as follows:

			$ fork		# starts clock
			$ ps		# find pid for clock0
			$ bg -p <pid>
			$ fg -p <pid>
			$ kill -p <pid>

kernel/kernel.s:
	rename suspend_task to task_suspend

kernel/schedule.s:
	rename suspend_task to task_suspend, and continue_task
	to task_resume.

	add cmd_suspend and cmd_resume and recode cmd_kill.

kernel/print.s:
	change macros PRINTc_ and PRINTLNc_ - deprecated as esi
	is preserved. Fixes weird GAS compilations when PRINTc_ 1,"'".

kernel/shell.s:
	add 'bg' (cmd_suspend) and 'fg' (cmd_resume). No stacking,
	and takes pid argument.