↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

paging.h
/*
EPOS
http://www.atanaslaskov.com/epos/

FILE:        paging.h
DESCRIPTION: Implementation of IA32 paging
 
Copyright (c) 2006, Atanas Laskov
All rights reserved.

BSD LICENSE
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. 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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 ATANAS LASKOV 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.
*/


// Some defaults
//
#define PAGE_ENTIRE_CNT 1024
#define PAGE_SIZE (4*1024)

#define PAGE_PRESENT        1
#define PAGE_NOT_PRESENT    0

#define PAGE_READONLEY  0
#define PAGE_RW         1

#define PAGE_USER       1
#define PAGE_SUPERVISOR 0

#define PAGE_PWT_WRITETHROUGH   1
#define PAGE_PWT_WRITEBACK      0

#define PAGE_DISABLECASH 1
#define PAGE_ENABLECASH  0

#define PAGE_4K 0

#define PAGE_GLOBAL    1
#define PAGE_NOTGLOBAL 0

#define NOALIGN __attribute__((packed))

#define PAGE_OK                 0
#define PAGE_OUT_OF_DIRECTORY   1

#define PAGING_DIR_KERNEL   0
#define PAGING_DIR_PROCESS0 1

// Page directory and page table descriptors
//
struct NOALIGN PageDirectoryEntire{
    unsigned long present     : 1;
    unsigned long rw             : 1;
    unsigned long us             : 1;
    unsigned long pwt        : 1;
    unsigned long cashdisable : 1;
    unsigned long accessed    : 1;
    unsigned long reserved    : 1;
    unsigned long pgsize      : 1;
    unsigned long global      : 1;
    unsigned long available   : 3;
    unsigned long page_base   : 20;
};

struct NOALIGN PageTableEntire{
    unsigned long present     : 1;
    unsigned long rw             : 1;
    unsigned long us             : 1;
    unsigned long pwt        : 1;
    unsigned long cashdisable : 1;
    unsigned long accessed    : 1;
    unsigned long dirty       : 1;
    unsigned long reserved    : 1;
    unsigned long global      : 1;
    unsigned long available   : 3;
    unsigned long page_base   : 20;
};

// Initialize paging
void initPaging();

// Resize page directory
void pageDirectorySize(unsigned long dwDirectoryIndex, unsigned long dwNewSize);

// Free page directory
void pageDirectoryFree(unsigned long dwDirectoryIndex);

// Switch to a different page directory mapping
void pageDirectorySwitch(unsigned long dwDirectoryIndex);

// Map page directory to physical memory
void pageMapPages(unsigned long dwDirectory, unsigned long dwLogbase,
    unsigned long dwPhysbase, unsigned long dwSize);

// Get phyisical address corresponding to page
unsigned pageGetPhysicalAddress(unsigned long dwDirectory,
        unsigned long dwLogAdr, unsigned long *pPhysicalAdr);
   
// Form linear memory address
#define PAGING_LINEARADR(dir, table, offset) (((unsigned long)(dir))*0x400000 + \
((unsigned long)(table))*0x1000 + ((unsigned long)(offset)))