rev |
line source |
nuclear@7
|
1 #include <string.h>
|
nuclear@7
|
2 #include "segm.h"
|
nuclear@7
|
3
|
nuclear@8
|
4 /* bits for the 3rd 16bt part of the descriptor */
|
nuclear@7
|
5 #define BIT_ACCESSED (1 << 8)
|
nuclear@7
|
6 #define BIT_WR (1 << 9)
|
nuclear@7
|
7 #define BIT_RD (1 << 9)
|
nuclear@7
|
8 #define BIT_EXP_DOWN (1 << 10)
|
nuclear@7
|
9 #define BIT_CONFORMING (1 << 10)
|
nuclear@7
|
10 #define BIT_CODE (1 << 11)
|
nuclear@7
|
11 #define BIT_NOSYS (1 << 12)
|
nuclear@7
|
12 #define BIT_PRESENT (1 << 15)
|
nuclear@7
|
13
|
nuclear@8
|
14 /* bits for the last 16bit part of the descriptor */
|
nuclear@7
|
15 #define BIT_BIG (1 << 6)
|
nuclear@7
|
16 #define BIT_DEFAULT (1 << 6)
|
nuclear@7
|
17 #define BIT_GRAN (1 << 7)
|
nuclear@7
|
18
|
nuclear@7
|
19 enum {TYPE_DATA, TYPE_CODE};
|
nuclear@7
|
20
|
nuclear@7
|
21 static void segm_desc(desc_t *desc, uint32_t base, uint32_t limit, int dpl, int type);
|
nuclear@7
|
22
|
nuclear@8
|
23 /* these functions are implemented in segm-asm.S */
|
nuclear@8
|
24 void setup_selectors(uint16_t code, uint16_t data);
|
nuclear@8
|
25 void set_gdt(uint32_t addr, uint16_t limit);
|
nuclear@7
|
26
|
nuclear@8
|
27
|
nuclear@8
|
28 /* our global descriptor table */
|
nuclear@7
|
29 static desc_t gdt[4];
|
nuclear@7
|
30
|
nuclear@7
|
31
|
nuclear@7
|
32 void init_segm(void)
|
nuclear@7
|
33 {
|
nuclear@7
|
34 memset(gdt, 0, sizeof gdt);
|
nuclear@7
|
35 segm_desc(gdt + SEGM_KCODE, 0, 0xffffffff, 0, TYPE_CODE);
|
nuclear@7
|
36 segm_desc(gdt + SEGM_KDATA, 0, 0xffffffff, 0, TYPE_DATA);
|
nuclear@7
|
37
|
nuclear@7
|
38 set_gdt((uint32_t)gdt, sizeof gdt - 1);
|
nuclear@7
|
39
|
nuclear@7
|
40 setup_selectors(selector(SEGM_KCODE, 0), selector(SEGM_KDATA, 0));
|
nuclear@7
|
41 }
|
nuclear@7
|
42
|
nuclear@8
|
43 /* constructs a GDT selector based on index and priviledge level */
|
nuclear@7
|
44 uint16_t selector(int idx, int rpl)
|
nuclear@7
|
45 {
|
nuclear@7
|
46 return (idx << 3) | (rpl & 3);
|
nuclear@7
|
47 }
|
nuclear@7
|
48
|
nuclear@7
|
49 static void segm_desc(desc_t *desc, uint32_t base, uint32_t limit, int dpl, int type)
|
nuclear@7
|
50 {
|
nuclear@7
|
51 desc->d[0] = limit & 0xffff; /* low order 16bits of limit */
|
nuclear@7
|
52 desc->d[1] = base & 0xffff; /* low order 16bits of base */
|
nuclear@7
|
53
|
nuclear@7
|
54 /* third 16bit part contains the last 8 bits of base, the 2 priviledge
|
nuclear@7
|
55 * level bits starting on bit 13, present flag on bit 15, and type bits
|
nuclear@7
|
56 * starting from bit 8
|
nuclear@7
|
57 */
|
nuclear@7
|
58 desc->d[2] = ((base >> 16) & 0xff) | ((dpl & 3) << 13) | BIT_PRESENT |
|
nuclear@7
|
59 BIT_NOSYS | (type == TYPE_DATA ? BIT_WR : (BIT_RD | BIT_CODE));
|
nuclear@7
|
60
|
nuclear@7
|
61 /* last 16bit part contains the last nibble of limit, the last byte of
|
nuclear@7
|
62 * base, and the granularity and deafult/big flags in bits 23 and 22 resp.
|
nuclear@7
|
63 */
|
nuclear@7
|
64 desc->d[3] = ((limit >> 16) & 0xf) | ((base >> 16) & 0xff00) | BIT_GRAN | BIT_BIG;
|
nuclear@7
|
65 }
|