Xv6 with picoc & Linkage editor  v1.0
The project delineate mutual cohesion between c library, linkage editor ( linker), interpreter and operating system by porting the same on xv6 kernel
mmu.h
00001 // This file contains definitions for the 
00002 // x86 memory management unit (MMU).
00003 
00004 // Eflags register
00005 #define FL_CF           0x00000001      // Carry Flag
00006 #define FL_PF           0x00000004      // Parity Flag
00007 #define FL_AF           0x00000010      // Auxiliary carry Flag
00008 #define FL_ZF           0x00000040      // Zero Flag
00009 #define FL_SF           0x00000080      // Sign Flag
00010 #define FL_TF           0x00000100      // Trap Flag
00011 #define FL_IF           0x00000200      // Interrupt Enable
00012 #define FL_DF           0x00000400      // Direction Flag
00013 #define FL_OF           0x00000800      // Overflow Flag
00014 #define FL_IOPL_MASK    0x00003000      // I/O Privilege Level bitmask
00015 #define FL_IOPL_0       0x00000000      //   IOPL == 0
00016 #define FL_IOPL_1       0x00001000      //   IOPL == 1
00017 #define FL_IOPL_2       0x00002000      //   IOPL == 2
00018 #define FL_IOPL_3       0x00003000      //   IOPL == 3
00019 #define FL_NT           0x00004000      // Nested Task
00020 #define FL_RF           0x00010000      // Resume Flag
00021 #define FL_VM           0x00020000      // Virtual 8086 mode
00022 #define FL_AC           0x00040000      // Alignment Check
00023 #define FL_VIF          0x00080000      // Virtual Interrupt Flag
00024 #define FL_VIP          0x00100000      // Virtual Interrupt Pending
00025 #define FL_ID           0x00200000      // ID flag
00026 
00027 // Control Register flags
00028 #define CR0_PE          0x00000001      // Protection Enable
00029 #define CR0_MP          0x00000002      // Monitor coProcessor
00030 #define CR0_EM          0x00000004      // Emulation
00031 #define CR0_TS          0x00000008      // Task Switched
00032 #define CR0_ET          0x00000010      // Extension Type
00033 #define CR0_NE          0x00000020      // Numeric Errror
00034 #define CR0_WP          0x00010000      // Write Protect
00035 #define CR0_AM          0x00040000      // Alignment Mask
00036 #define CR0_NW          0x20000000      // Not Writethrough
00037 #define CR0_CD          0x40000000      // Cache Disable
00038 #define CR0_PG          0x80000000      // Paging
00039 
00040 #define CR4_PSE         0x00000010      // Page size extension
00041 
00042 #define SEG_KCODE 1  // kernel code
00043 #define SEG_KDATA 2  // kernel data+stack
00044 #define SEG_KCPU  3  // kernel per-cpu data
00045 #define SEG_UCODE 4  // user code
00046 #define SEG_UDATA 5  // user data+stack
00047 #define SEG_TSS   6  // this process's task state
00048 
00049 //PAGEBREAK!
00050 #ifndef __ASSEMBLER__
00051 // Segment Descriptor
00052 struct segdesc {
00053   uint lim_15_0 : 16;  // Low bits of segment limit
00054   uint base_15_0 : 16; // Low bits of segment base address
00055   uint base_23_16 : 8; // Middle bits of segment base address
00056   uint type : 4;       // Segment type (see STS_ constants)
00057   uint s : 1;          // 0 = system, 1 = application
00058   uint dpl : 2;        // Descriptor Privilege Level
00059   uint p : 1;          // Present
00060   uint lim_19_16 : 4;  // High bits of segment limit
00061   uint avl : 1;        // Unused (available for software use)
00062   uint rsv1 : 1;       // Reserved
00063   uint db : 1;         // 0 = 16-bit segment, 1 = 32-bit segment
00064   uint g : 1;          // Granularity: limit scaled by 4K when set
00065   uint base_31_24 : 8; // High bits of segment base address
00066 };
00067 
00068 // Normal segment
00069 #define SEG(type, base, lim, dpl) (struct segdesc)    \
00070 { ((lim) >> 12) & 0xffff, (uint)(base) & 0xffff,      \
00071   ((uint)(base) >> 16) & 0xff, type, 1, dpl, 1,       \
00072   (uint)(lim) >> 28, 0, 0, 1, 1, (uint)(base) >> 24 }
00073 #define SEG16(type, base, lim, dpl) (struct segdesc)  \
00074 { (lim) & 0xffff, (uint)(base) & 0xffff,              \
00075   ((uint)(base) >> 16) & 0xff, type, 1, dpl, 1,       \
00076   (uint)(lim) >> 16, 0, 0, 1, 0, (uint)(base) >> 24 }
00077 #endif
00078 
00079 #define DPL_USER    0x3     // User DPL
00080 
00081 // Application segment type bits
00082 #define STA_X       0x8     // Executable segment
00083 #define STA_E       0x4     // Expand down (non-executable segments)
00084 #define STA_C       0x4     // Conforming code segment (executable only)
00085 #define STA_W       0x2     // Writeable (non-executable segments)
00086 #define STA_R       0x2     // Readable (executable segments)
00087 #define STA_A       0x1     // Accessed
00088 
00089 // System segment type bits
00090 #define STS_T16A    0x1     // Available 16-bit TSS
00091 #define STS_LDT     0x2     // Local Descriptor Table
00092 #define STS_T16B    0x3     // Busy 16-bit TSS
00093 #define STS_CG16    0x4     // 16-bit Call Gate
00094 #define STS_TG      0x5     // Task Gate / Coum Transmitions
00095 #define STS_IG16    0x6     // 16-bit Interrupt Gate
00096 #define STS_TG16    0x7     // 16-bit Trap Gate
00097 #define STS_T32A    0x9     // Available 32-bit TSS
00098 #define STS_T32B    0xB     // Busy 32-bit TSS
00099 #define STS_CG32    0xC     // 32-bit Call Gate
00100 #define STS_IG32    0xE     // 32-bit Interrupt Gate
00101 #define STS_TG32    0xF     // 32-bit Trap Gate
00102 
00103 // A virtual address 'la' has a three-part structure as follows:
00104 //
00105 // +--------10------+-------10-------+---------12----------+
00106 // | Page Directory |   Page Table   | Offset within Page  |
00107 // |      Index     |      Index     |                     |
00108 // +----------------+----------------+---------------------+
00109 //  \--- PDX(va) --/ \--- PTX(va) --/ 
00110 
00111 // page directory index
00112 #define PDX(va)         (((uint)(va) >> PDXSHIFT) & 0x3FF)
00113 
00114 // page table index
00115 #define PTX(va)         (((uint)(va) >> PTXSHIFT) & 0x3FF)
00116 
00117 // construct virtual address from indexes and offset
00118 #define PGADDR(d, t, o) ((uint)((d) << PDXSHIFT | (t) << PTXSHIFT | (o)))
00119 
00120 // Page directory and page table constants.
00121 #define NPDENTRIES      1024    // # directory entries per page directory
00122 #define NPTENTRIES      1024    // # PTEs per page table
00123 #define PGSIZE          4096    // bytes mapped by a page
00124 
00125 #define PGSHIFT         12      // log2(PGSIZE)
00126 #define PTXSHIFT        12      // offset of PTX in a linear address
00127 #define PDXSHIFT        22      // offset of PDX in a linear address
00128 
00129 #define PGROUNDUP(sz)  (((sz)+PGSIZE-1) & ~(PGSIZE-1))
00130 #define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
00131 
00132 // Page table/directory entry flags.
00133 #define PTE_P           0x001   // Present
00134 #define PTE_W           0x002   // Writeable
00135 #define PTE_U           0x004   // User
00136 #define PTE_PWT         0x008   // Write-Through
00137 #define PTE_PCD         0x010   // Cache-Disable
00138 #define PTE_A           0x020   // Accessed
00139 #define PTE_D           0x040   // Dirty
00140 #define PTE_PS          0x080   // Page Size
00141 #define PTE_MBZ         0x180   // Bits must be zero
00142 #define PROT_WRITE PTE_W
00143 #define MAP_PRIVATE PTE_P|PTE_W|PTE_U
00144 #define MAP_ANONYMOUS MAP_PRIVATE
00145 
00146 // Address in page table or page directory entry
00147 #define PTE_ADDR(pte)   ((uint)(pte) & ~0xFFF)
00148 
00149 #ifndef __ASSEMBLER__
00150 typedef uint pte_t;
00151 
00152 // Task state segment format
00153 struct taskstate {
00154   uint link;         // Old ts selector
00155   uint esp0;         // Stack pointers and segment selectors
00156   ushort ss0;        //   after an increase in privilege level
00157   ushort padding1;
00158   uint *esp1;
00159   ushort ss1;
00160   ushort padding2;
00161   uint *esp2;
00162   ushort ss2;
00163   ushort padding3;
00164   void *cr3;         // Page directory base
00165   uint *eip;         // Saved state from last task switch
00166   uint eflags;
00167   uint eax;          // More saved state (registers)
00168   uint ecx;
00169   uint edx;
00170   uint ebx;
00171   uint *esp;
00172   uint *ebp;
00173   uint esi;
00174   uint edi;
00175   ushort es;         // Even more saved state (segment selectors)
00176   ushort padding4;
00177   ushort cs;
00178   ushort padding5;
00179   ushort ss;
00180   ushort padding6;
00181   ushort ds;
00182   ushort padding7;
00183   ushort fs;
00184   ushort padding8;
00185   ushort gs;
00186   ushort padding9;
00187   ushort ldt;
00188   ushort padding10;
00189   ushort t;          // Trap on task switch
00190   ushort iomb;       // I/O map base address
00191 };
00192 
00193 // PAGEBREAK: 12
00194 // Gate descriptors for interrupts and traps
00195 struct gatedesc {
00196   uint off_15_0 : 16;   // low 16 bits of offset in segment
00197   uint cs : 16;         // code segment selector
00198   uint args : 5;        // # args, 0 for interrupt/trap gates
00199   uint rsv1 : 3;        // reserved(should be zero I guess)
00200   uint type : 4;        // type(STS_{TG,IG32,TG32})
00201   uint s : 1;           // must be 0 (system)
00202   uint dpl : 2;         // descriptor(meaning new) privilege level
00203   uint p : 1;           // Present
00204   uint off_31_16 : 16;  // high bits of offset in segment
00205 };
00206 
00207 // Set up a normal interrupt/trap gate descriptor.
00208 // - istrap: 1 for a trap (= exception) gate, 0 for an interrupt gate.
00209 //   interrupt gate clears FL_IF, trap gate leaves FL_IF alone
00210 // - sel: Code segment selector for interrupt/trap handler
00211 // - off: Offset in code segment for interrupt/trap handler
00212 // - dpl: Descriptor Privilege Level -
00213 //        the privilege level required for software to invoke
00214 //        this interrupt/trap gate explicitly using an int instruction.
00215 #define SETGATE(gate, istrap, sel, off, d)                \
00216 {                                                         \
00217   (gate).off_15_0 = (uint)(off) & 0xffff;                \
00218   (gate).cs = (sel);                                      \
00219   (gate).args = 0;                                        \
00220   (gate).rsv1 = 0;                                        \
00221   (gate).type = (istrap) ? STS_TG32 : STS_IG32;           \
00222   (gate).s = 0;                                           \
00223   (gate).dpl = (d);                                       \
00224   (gate).p = 1;                                           \
00225   (gate).off_31_16 = (uint)(off) >> 16;                  \
00226 }
00227 
00228 #endif
 All Data Structures