/************************************************************************************
* Copyright (c) 2007, Michael Lazear												*
* All rights reserved.																*
*																					*
* Redistribution and use in source and binary forms, with or without				*
* modification, are permitted provided that the following conditions are met:		*
*     * Redistributions of source code must retain the above copyright				*
*       notice, this list of conditions and the following disclaimer.				*
*     * Redistributions in binary form must reproduce the above copyright			*
*       notice, this list of conditions and the following disclaimer in the			*
*       documentation and/or other materials provided with the distribution.		*
*     * Neither the name of the <Phoenix Operating System> nor the					*
*       names of its contributors may be used to endorse or promote products		*
*       derived from this software without specific prior written permission.		*
*																					*
* THIS SOFTWARE IS PROVIDED BY <Michael Lazear> ``AS IS'' AND ANY					*
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED			*
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE			*
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY				*
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES		*
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;		*
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND		*
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT		*
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS		*
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.						*
*************************************************************************************/

#include <kernel.h>
#include <bool.h>
#include <types.h>

extern uint_t write_cr3( uint_t cr3 );
extern uint_t write_cr0( uint_t cr0 );
extern uint_t read_cr0();

ulong_t kerneldir[1024] __attribute__((aligned(4096)));	// 1024 tables w/ 1024 pages, make sure it is on a boundary
ulong_t *currentdir;

void switch_dir()
{
	if( !currentdir )
		return;
	/* write the current directory's address to CR3 */
	write_cr3( currentdir );
}

void init_paging()
{
	ulong_t pt[1024];	/* pagetable */
	uint_t i;
	/* set the kernel directory as the current */
	currentdir = &kerneldir;

	/* identity map the first 4 mb (1:1) */
	for( i = 0; i < 1024; i++ )
		pt[i] = (i*4096) | 3;//pagetable[i] = (i * 4096) | 3;

	/* setup the kernel page directory */
	for( i = 1; i < 1024; i++ )
		currentdir[i] = 0 | 2;
	currentdir[0] = pt;//pagetable;
	/* point back to self */
	currentdir[0] = currentdir[0] | ;

	/* set currentdir as page directory */
	switch_dir();
	/* enable paging */
	write_cr0( read_cr0() | 0x80000000 );
		
}
	