Paging improvements. Task paging isolation.

	SUMMARY:

		Kernel page tables are allocated from the same pool as those
		for tasks.

		Tasks, already running in their own paging structure, now run
		isolated from the kernel's paging tables. The page directory
		is no longer copied from the kernel, but only contains the
		shared memory (low 4Mb), and the paging tables for the task.
		The task is the only one using it's pages as page tables.
		It can, however, make any page in it's 4Mb region accessible.

kernel/mem.s:
	cmd_mem$: update printing paging info: calc and print the number
	  of free pages; move printing the physical range of the high pages
	  from 'mem -k' to 'mem -s'.

	malloc_page_phys: use a new mem_pages_sem semaphore to lock the
	mem_pages and mem_pages_free arrays, instead of MUTEX_MEM, which
	causes a halt condition when allocating these arrays inside the mutex.

kernel/paging.s:
	Fixed some typo's. Renamed P[DT]E_FLAG_R to _FLAG_RW as it indicates
	write-enabled.

	Rewrite paging_init. Main change is using malloc_page_phys instead
	of manually reserving the space after stack for kernel pages.
	Only the page directory is allocated with malloc_page_phys directly.
	The page tables are allocated using the new method
	paging_alloc_page_idmap, which takes care of everything to return a
	usable free page, already mapped.
	Now, page allocation is uniform/standard, both for kernel and tasks.
	The 0..4mb range is identity mapped as before. The page directory
	itself is identity mapped aswell to allow access after paging is
	enabled.

	paging_enable/disable: write protection bit also set/cleared. Remove
	code garbaging the page dir and the page following it as it references
	the old way of allocating the kernel page tables.

	paging_idmap_4m: removed. Driver code that used this method
	has been updated to call paging_idmap_memrange instead.

	paging_idmap_page: assert the page address is page-aligned.
	Fix two bugs: 1) the PTE ptr's flags were not masked out;
	2) the index in the PTE used the wrong bits, always yielding PDE 0.
	Also, trigger int 3 when the PT in the PDE for the PTE is 0, as it
	can then not be mapped. For such instances, call
	paging_idmap_page_pt_alloc, which allocates and maps a page table
	to map the page if it is not present.

	add paging_idmap_memrange: splits the memrange into pages and identity
	maps them one by one. Used by drivers to enable access to mmio.

	paging_map_4k: removed; functionality taken over by paging_idmap_page.

	paging_alloc_page_idmap: allocates a page and identity maps it.
	If it cannot map due to the 4Mb memory area of the page not being
	present in the page directory, it will use the page as a page directory,
	identity map the page, and recurse. See the comment in the method
	for some observations.
	The _idmap suffix indicates that the allocated page will be identity
	mapped - this means it can be written to, and the method does so for
	two reaons: 1) for use as a page table; 2) to clear the page.

	add paging_idmap_page_pt_alloc: this method is similar to the one above,
	except that it takes a page as argument instead of allocating it.
	It's purpose is the same as paging_idmap_page (to identity map a page),
	but it allows to allocate an additional page to use as a page table
	in order to do so. Note that THIS method CAN result in a page table
	being used to map a 4Mb range it is not part of.

	paging_show_usage1$: renamed to paging_show_usage_.
	paging_show_usage: recoded to re-use as much code as possible with:
	paging_show_structure: new method, to show the PDE and non-0 PTE's.
	paging_show_structure_: same, but takes esi as a pointer to the
	page directory, so that other page directories beside the current
	one can also be inspected.
	Where the _show_usage methods will report the access to pages
	in all memory ranges (including 0..4Mb), the _show_structure
	method will not show the first 4Mb since it is uninteresting,
	as it is contiguously identity mapped.

kernel/schedule.s:
	schedule_far: update the scheduling disabled handler, called
	from mutex code predominantly, and add an int 3 call.

	task_setup_paging: recode.
	* Use paging_alloc_page_idmap instead of malloc_page_phys + idmap_4m.
	* uses the current task CR3 as a source for the low 4Mb PT copy,
	  and clears the dirty flag aswell.
	* The PDE is no longer copied, but reconstructed from scratch:
	  - The copy of the 0..4Mb page dir is mapped (task_page_table_lo).
	  - The task_page_table_hi is mapped and will contain the PTs:
	    o task_page_dir
	    o task_page_table_lo
	    o task_page_table_hi

	      NOTE that these are needed in order to fork new tasks from
	      any task without switching out CR3 for the kernel.

kernel/shell.s:
	Add cmd_paging, allowing to show both the structure and the usage
	for the paging structure of any task. (default: current task).

kernel/usb.s:
kernel/usb_ohci.s:
kernel/vmware/svga2.s:
	For all three: call paging_idmap_memrange instead of pagign_idmap_4m.
	In practice, this results in page-table mapped memory (using 4kb
	of memory per memory range instead of a PDE with PDE_FLAG_S).

	For svga2.s: some cleanup (removal of debug); print framebuffer
	and fifo address ranges and size.
Fix nprinthex.
Fix keyboard semaphore/buffer consistency.

kernel/keyboard.s:
	k_get$ would jump to kb_remove$ if the buffer contains keys,
	but would not adjust the console_kb_sem, resulting in false positives.
	Now, kb_remove$ decrements the semaphore (representing the key-count).
Fix DEBUG_REGDIFF printing unchanged esp.
Allow int3 CPL>0; Preserve ebp in kernel_callgate.

kernel/gdt.s:
	minor documentation updates; flags more explicit (even though
	value doesn't change).

	kernel_callgate: do not use ebp in the call to the caller,
	so that it can load the stack pointer before the call and have it
	usable.

kernel/gdt.s:
	exception handler: more compact - don't print mem used/reserved.
	print kernel page directory address on page fault. Print PDE
	for linaddr (cr2) instead of it's address.

	set DPL of int3 in the IDT to RING3.
Allow CPL>0 to send network packets.

kernel/net/net.s:
	add net_buffer_send which does a kernel call for CPL>0.
	Update NET_BUFFER_SEND to call the method.
Allow CPL>0 to call get_time_ms (PIT).

kernel/pit.s:
	enter kernelmode if needed.
Paging: don't map with P[TD]E_FLAG_U.

kernel/paging.s:
	map all pages without the U flag.

	make flag printing more legible.

	fix printing PTE range.
Debug macro fixes.

kernel/debug.s:
	DEBUG_BYTE: fix pushing the 8bit value if it is a reg8. It used to
	only work properly if the argument was al, bl, cl, or dl (or a mem ref).

	DEBUG_DWORD: expect the argument to be pushable. This optimizes memory
	references being debugged as they are not reg32 but can be pushed
	directly. It is now not possible to directly debug cr3, dr0 etc..
	Also adjust the value if esp is the argument.

   XXX remove commented out lines

kernel/hwdebug.s:
	store dr[01236] into edx before DEBUG_DWORD.

kernel/shell.s:
	cmd_debug: DEBUG_DWORD dr[01237] via edx.
Debugger: swap CR3 (disabled)

kernel/debugger.s:
	allow to switch out CR3 for the kernel PD.
	Currently disabled.
Keyboard: assert console_kb != 0
Shell: cmd_int, cmd_paging improvements.

kernel/shell.s:
	cmd_int: print cs:eip, ss:esp for comparison with exception handler.

	cmd_paging: switch to kernel context (CPL0, CR3) so all tasks
	can show the paging structure. (for debug only!)
Scheduling: allocate privileged stack per task.

kernel/schedule.s:
	Implement a stack-page allocation scheme. Multiple stacks per page.
	The pages reside in the kernel page area (high mem). Each task
	(CPL0 also, but this is not needed, yet enabled for testing) will
	have a CPL0 stack. The schedule_isr will update the TSS ESP0 field.

	task descriptor: add task_stack_esp0, task_stack0_top (for free),
	  task_stack_ss0, and task_stack0_bitindex (debugging).

	static data: add scheduler_prev_task_idx to be able to compare CPL's.
	scheduler_get_task: store interrupted task idx.

	scheduler_init: update the kernel task's task_stack ss0/esp0 from the
	TSS.

	increment the PID counter after registering the kernel task: fixes
	the '2 processes have PID 0'.

  xxx	schedule_isr: before switching stacks, set CR3 to kernel PD.
	  this is needed in case the interrupted tasks CR3 is pointing to
	  an unprivileged paging structure, and the new task's CR3 is
	  privileged (i.e. it is in kernel mode using the kernel's stack pages)
	  - after this, the task's cr3 is loaded.

  	add task_print_stack$(eax=idx): print pid, flags, name, and registers
	  on the stack (part of the debug code, where with wrong stack, popping
	  a segment register would cause a #GP).

  	task done: free privileged stack.

	schedule_task: swap out CR3 for kernel PD, and allocate a privileged
	stack (stack pages).

	add privileged stack management: (alloc|free)_task_priv_stack.

	Update the PDE/PTE flags in task_setup_paging:
  	* low 4Mb PTEs: FLAG_U | FLAG_RW
	* low 4Mb PDE: FLAG_RW, but not FLAG_U (no CPL3 access)
	* task_page_dir: U, not RW
	* low 4mb page table idmap: U + RW.
	* task page tab hi: make RW, not U.
Scheduling, Paging, Interrupts

	SUMMARY
	=======
	Add interrupt task gates for Page Fault and Double Fault.
	The double fault task gate ensures that the error gets displayed
	by using a well-known kernel context.

DOC/CallingConvention.txt:
	add ideas for CPL3 kernel API.

DOC/Paging.txt:
	Add ideas on paging.

DOC/Scheduling.txt:
	add problem/solution on scheduling and privilege levels. Details
	the privileged stack algorithm.

TODO:
	remove the am79c971 isr comment - it was false.
	Add TODO's for the new scheduling and paging implementations,
	and some idea's.

kernel/gdt.s:
	* make the main TSS DPL3. [verify if this has any effect]
	* remove GDT_tss2 / SEL_tss2.
	* add GDT_kernelGate, a 3rd callgate for testing.
	* add GDT_ring0SS for TSS_SS0 usage.
	* add GDT_tss_(pf|df), SEL_tss_(pf|df): page/double fault int gates.

	init_gdt16:
		* init GDT_ring0SS segment
		* use 108 for the TSS limit as per Intel documentation
		  (it mentions that on task switch the TSS limit is checked to
		  be >= 108, even though the structure is 104 bytes).
		* initialize GDT_tss_(pf|df) base/limit.
		* initialize GDT_kernelCallGate with kernel_callgate_3 offset.
	* add kernel_callgate_3.

kernel/idt.s:
	* add constants EX_* (EX_GP, EX_PF etc).
	* jmp_table_target:
		* if errorcode references GDT, print details using
		  debug_pring_gdt_descriptor.
		* enter textmode if needed (works in CPL0 tasks).

	* update debug_print_tss: takes dx argument.
	* add halt after ics$ (invalid code sel).
	* add debug_print_gdt_descriptor: print type.
	* update debug_print_tss$: call print_tss (tss.s) first; print proper
	  values.

	* add ex_(pf|df)_task_isr: a task gate (using TSS_(PF|DF)), called from
	  the IDT for #Pf and #DF.

	* init_idt: initialize IDT and TSS for #PF and #DF.

kernel/kernel.s:
	* update reference to the main TSS using the tss struct.
	* add (disabled) code to fork a console task rather than calling shell.

kernel/paging.s:
	paging_init: update TSS, TSS_DF and TSS_PF with CR3.

	paging_print_$: print CR3 and kernel PD.

	add paging_show_task_struct: callable from CPL>0.

kernel/tss.s:
	* add tss struct and remove the explicit TSS labels. Access now
	  done with [TSS + tss_REG].
	* remove TSS2.
	* add TSS_PF, TSS_DF.

	* init_tss16: recode; use a loop to configure CR3 and all TSS segment
	  selectors and EIP (the task's starting point) to a new default_task
	  handler, and CR3.

	* add default_task for semi configured TSS-es.
	* add print_tss: flags, stack, and link. Print all linked TSS also.
	* update refs to TSS0 to TSS + tss_...
Kernel: add disabled console-shell fork.

[commit to clean working directory]

kernel/kernel.s:
	allow to run the kernel console as a task with different CPL.
	Currently disabled. Doing this fork would remove the need
	for an idle task.
Implement KAPI (Kernel API) framework - paging.

	SUMMARY (see DOC/CallingConvention.txt)
	=======
	A segment selector is reserved for kernel calls. It's base is mapped to
	an unmapped page, so that any access will cause a page fault.
	The page fault handler will then take care of handling kernel calls.
	The handler is a task, and thus does hardware task switching.
	The fault address is checked against the designated kernel api page,
	and the offset in it is the API number, which is looked up in a
	table containing the name and address.

kernel/Makefile:
	register kapi.s

kernel/defines:
	declare 3 data subsections for the KAPI symbol table and method ptrs.

kernel/fs.s:
	declare two API methods, for testing.

kernel/gdt.s:
	add GDT_kapi/SEL_kapi: the kernel API code selector.

kernel/idt.s:
	add some more detailed priting in the default page fault handler.

kernel/kapi.s:
	declare 3 data sections, two for a stringtable (offsets and stringdata)
	and one for the method pointers.

	add macro KAPI_DECLARE to be used to declare kernel API method entry
	points.

	add cmd_kapi: print the kernel API methods / ptrs.

	add kapi_init: makes sure that the 4Mb range of the KAPI page is
	mapped (but the page table remains empty), sets SEL_kapi.base to it,
	and updates the TSS_PF's EIP to point to kapi_pf - the kernel api
	call handler.

	kapi_pf: page fault task. It reads cr2 to get the fault address,
	checks if it is the KAPI_PAGE, and prints the API from the KAPI table.
	It updates the linked TSS's CS/EIP/ESP to simulate a return.

	add cmd_kapi_test: calls an address in SEL_kapi.

kernel/kernel.s:
	record kapi.s; include print.s with DEFINE=0 before it.
	declare the end offsets for the data_kapi_*.

	call kapi_init after paging_init.

kernel/shell.s:
	declare cmd_kapi and cmd_kapi_test.