kern

view src/segm.c @ 8:78d5c304ddd0

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