↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

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

FILE:        gdt.h
DESCRIPTION: Handling the IA32 Global Descriptor Table
 
BSD LICENSE
Copyright (c) 2006, Atanas Laskov
All rights reserved.
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.
*/

#ifndef GDT_H
#define GDT_H

//GDTR format
//
struct GDTR{
    unsigned short wSize;
    unsigned long  dwStart;
};

//Descriptor format
//
struct GDTDescriptor{
    unsigned short wLimit15_0;
    unsigned short wBase15_0;
    unsigned char  bBase23_16;
    unsigned char  bAccess;
    unsigned char  bLimit19_16 : 4;
    unsigned char  bFlags : 4;
    unsigned char  bBase31_24;
};

// Descriptor flags (and interpretation if not set)
//
#define GDTD_ACCESS_TYPE_LDT
#define GDTD_ACCESS_TYPE_CODE             8 /* otherwise data */
#define GDTD_ACCESS_TYPE_DATA_EXPANDDOWN  4 /* otherwise expand-up */
#define GDTD_ACCESS_TYPE_CODE_COMFORMING  4 /* otherwise non-conforming */
#define GDTD_ACCESS_TYPE_DATA_WRITEABLE   2 /* otherwise read-only */
#define GDTD_ACCESS_TYPE_CODE_READABLE    2 /* otherwise exec-only */
#define GDTD_ACCESS_TYPE_ACCESSED         1 /* otherwise not accessed */

#define GDTD_ACCESS_SYSTEM     0x10 /* otherwise code/data */
#define GDTD_ACCESS_RING3      0x60 /* otherwise r0 */
#define GDTD_ACCESS_PRESENT    0x80 /* otherwise not present */

#define GDTD_FLAGS_32BIT          4 /* otherwise 16-bit */
#define GDTD_FLAGS_PAGEGRANULAR   8 /* otherwise byte granular */

//GDT format
struct GDT{
    struct GDTDescriptor  *desc;
};

//GDT descriptor indexes
//
#define INDEX_GDT_NULL        0
#define INDEX_GDT_LINEAR_DATA 1
#define INDEX_GDT_CODE        2
#define INDEX_GDT_DATA        3
#define INDEX_GDT_TSS         4
#define INDEX_GDT_LDT0        5

//LDT format
struct LDT{
    struct GDTDescriptor desc[2];
};

//LDT descriptor indexes
#define INDEX_LDT_CODE 0
#define INDEX_LDT_DATA 1

//Linear/LDT data access
//
unsigned char gdt_linear_getb(unsigned long ofs);
void          gdt_linear_setb(unsigned long ofs, char byte);
unsigned char gdt_ldtdatas_getb(unsigned long ofs);
void          gdt_ldtdatas_setb(unsigned long ofs, char byte);
unsigned long gdt_linear_stack_getdw(unsigned long ofs);

//Initialization
void gdtInit();

//Descriptor access
//
struct GDTDescriptor* gdtGetDescriptorGDT(unsigned long ndesc);
struct GDTDescriptor* gdtGetDescriptorLDT(unsigned long nldt, unsigned long ndesc);

//Descriptor manipulation
//
void  gdtSetBase(struct GDTDescriptor* pDesc, unsigned long dwBase);
void  gdtSetLimit(struct GDTDescriptor* pDesc, unsigned long dwLimit);
void  gdtFormPointer(struct GDTDescriptor* pDesc, void *DescLocalPtr, void** pptr);

//LDT data transfer
//
void gdtLDTDatasGetBufer(unsigned long ofs, unsigned char*pBuf, unsigned long size);
void gdtLDTDatasPutBufer(unsigned long ofs, unsigned char*pBuf, unsigned long size);

//I/O permission map manipulations
unsigned char*        gdt_getp_iomask();

#endif //#ifndef GDT_H