kern

annotate src/segm.c @ 10:b11a86695493

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