2 * General data structures and routines for segmentation
3 * Copyright (c) 2001, David H. Hovemeyer <daveho@cs.umd.edu>
6 * This is free software. You are permitted to use,
7 * redistribute, and modify it as specified in the file "COPYING".
11 * Source: _Protected Mode Software Architecture_ by Tom Shanley,
15 #ifndef GEEKOS_SEGMENT_H
16 #define GEEKOS_SEGMENT_H
18 #include <geekos/ktypes.h>
25 #define PACKED __attribute__((packed))
29 * The general format of a segment descriptor.
31 struct Segment_Descriptor {
32 ushort_t sizeLow PACKED ;
33 uint_t baseLow : 24 PACKED ;
34 uint_t type : 4 PACKED ;
35 uint_t system : 1 PACKED ;
36 uint_t dpl : 2 PACKED ;
37 uint_t present : 1 PACKED ;
38 uint_t sizeHigh : 4 PACKED ;
39 uint_t avail : 1 PACKED ;
40 uint_t reserved : 1 PACKED ; /* set to zero */
41 uint_t dbBit : 1 PACKED ;
42 uint_t granularity : 1 PACKED ;
43 uchar_t baseHigh PACKED ;
47 * Construct a segment selector.
48 * @param rpl requestor privilege level; should be KERNEL_PRIVILEGE
49 * for kernel segments and USER_PRIVILEGE for user segments
50 * @param segmentIsInGDT true if the referenced segment descriptor
51 * is defined in the GDT, false if it is defined in the LDT
52 * @param index index of the segment descriptor
53 * @return the segment selector
55 static __inline__ ushort_t Selector(int rpl, bool segmentIsInGDT, int index)
57 ushort_t selector = 0;
58 selector = (rpl & 0x3) | ((segmentIsInGDT ? 0 : 1) << 2) | ((index & 0x1FFF) << 3);
63 * Routines to initialize segment descriptors.
64 * Code and data segments must start on a page-aligned address
65 * and are sized in pages.
68 void Init_Null_Segment_Descriptor(struct Segment_Descriptor* desc);
70 void Init_Code_Segment_Descriptor(
71 struct Segment_Descriptor* desc,
76 void Init_Data_Segment_Descriptor(
77 struct Segment_Descriptor* desc,
82 void Init_TSS_Descriptor(struct Segment_Descriptor* desc, struct TSS* theTSS);
84 void Init_LDT_Descriptor(
85 struct Segment_Descriptor* desc,
86 struct Segment_Descriptor theLDT[],
90 #endif /* GEEKOS_SEGMENT_H */