aboutsummaryrefslogtreecommitdiff
path: root/i686/lgdt.c
blob: 473a91d96bb6d6ed089d41f3b85ff9435be84a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "gdt.h"

struct __attribute__((packed)) Pointer {
  uint16_t limit;
  uint32_t base;
};

static struct SegmentDescriptor_t segments[8] __attribute__((aligned(32)));

void
gdt_install()
{
  const struct Pointer ptr = {sizeof(segments) - 1, (unsigned)&segments};
  SegmentDescriptor(&segments[0], 0, 0, 0);             /* null segment */
  SegmentDescriptor(&segments[2], 0, 0xffffffff, 0x9a); /* ktext segment */
  SegmentDescriptor(&segments[3], 0, 0xffffffff, 0x92); /* kdata segment */

  __asm__("lgdt (%0)" : : "a"(&ptr));

  /* load the kernel data segment */
  __asm__("mov %0, %%ds; mov %0, %%es; mov %0, %%fs; mov %0, %%gs; mov %0, %%ss" : : "ax"(kdataDescriptor));

  /* load the kernel code segment */
  __asm__("ljmp %0, $1f\n1:" : : "i"(ktextDescriptor));
}