Fork me on GitHub

root/kernel/gdt.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. gdt_set_entry
  2. gdt_init

   1 /*
   2  * fiwix/kernel/gdt.c
   3  *
   4  * Copyright 2018, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/asm.h>
   9 #include <fiwix/const.h>
  10 #include <fiwix/types.h>
  11 #include <fiwix/segments.h>
  12 #include <fiwix/process.h>
  13 #include <fiwix/limits.h>
  14 #include <fiwix/string.h>
  15 
  16 struct seg_desc gdt[NR_GDT_ENTRIES];
  17 
  18 struct desc_r gdtr = {
  19         sizeof(gdt) - 1,
  20         (unsigned int)&gdt
  21 };
  22 
  23 static void gdt_set_entry(int num, unsigned int base_addr, unsigned int limit, char loflags, char hiflags)
  24 {
  25         num /= sizeof(struct seg_desc);
  26         gdt[num].sd_lolimit = limit & 0xFFFF;
  27         gdt[num].sd_lobase = base_addr & 0xFFFFFF;
  28         gdt[num].sd_loflags = loflags;
  29         gdt[num].sd_hilimit = (limit >> 16) & 0x0F;
  30         gdt[num].sd_hiflags = hiflags;
  31         gdt[num].sd_hibase = (base_addr >> 24) & 0xFF;
  32 }
  33 
  34 void gdt_init(void)
  35 {
  36         unsigned char loflags;
  37 
  38         gdt_set_entry(0, 0, 0, 0, 0);   /* null descriptor */
  39 
  40         loflags = SD_CODE | SD_CD | SD_DPL0 | SD_PRESENT;
  41         gdt_set_entry(KERNEL_CS, 0, 0xFFFFFFFF, loflags, SD_OPSIZE32 | SD_PAGE4KB);
  42         loflags = SD_DATA | SD_CD | SD_DPL0 | SD_PRESENT;
  43         gdt_set_entry(KERNEL_DS, 0, 0xFFFFFFFF, loflags, SD_OPSIZE32 | SD_PAGE4KB);
  44 
  45         loflags = SD_CODE | SD_CD | SD_DPL3 | SD_PRESENT;
  46         gdt_set_entry(USER_CS, 0, 0xFFFFFFFF, loflags, SD_OPSIZE32 | SD_PAGE4KB);
  47         loflags = SD_DATA | SD_CD | SD_DPL3 | SD_PRESENT;
  48         gdt_set_entry(USER_DS, 0, 0xFFFFFFFF, loflags, SD_OPSIZE32 | SD_PAGE4KB);
  49 
  50         loflags = SD_TSSPRESENT;
  51         gdt_set_entry(TSS, 0, sizeof(struct proc) - 1, loflags, SD_OPSIZE32);
  52 
  53         load_gdt((unsigned int)&gdtr);
  54 }

/* [previous][next][first][last][top][bottom][index][help] */