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

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