source: libcaca/trunk/kernel/boot/gdt.c @ 4158

Last change on this file since 4158 was 4158, checked in by Jean-Yves Lamoureux, 10 years ago
  • Fix indentation and add licensing and copyright headers to kernel
File size: 3.0 KB
Line 
1/*
2 *  libcaca       
3 *  libcaca       Colour ASCII-Art library
4 *  Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
5 *                2009 Jean-Yves Lamoureux <jylam@lnxscene.org>
6 *                All Rights Reserved
7 *
8 *  $Id: kernel.h 4154 2009-12-20 13:33:11Z jylam $
9 *
10 *  This library is free software. It comes without any warranty, to
11 *  the extent permitted by applicable law. You can redistribute it
12 *  and/or modify it under the terms of the Do What The Fuck You Want
13 *  To Public License, Version 2, as published by Sam Hocevar. See
14 *  http://sam.zoy.org/wtfpl/COPYING for more details.
15 */
16
17/*
18 *'freely' inspired by http://jojo.ouvaton.org/dossiers/boot_sector/tutorial02.html
19 * (actually, that's mostly copied, with minor compilation fixes)
20 */
21#include "kernel.h"
22#include "klibc.h"
23
24#define GDTBASE 0x800           /* Physical address of GDT */
25#define GDTSIZE 0xFF            /* Maximum table size (in entries) */
26
27/* Segment descriptor */
28struct gdtdesc
29{
30    u16 lim0_15;
31    u16 base0_15;
32    u8 base16_23;
33    u8 acces;
34    u8 lim16_19:4;
35    u8 other:4;
36    u8 base24_31;
37} __attribute__ ((packed));
38
39/* GDTR register */
40struct gdtr
41{
42    u16 limite;
43    u32 base;
44} __attribute__ ((packed));
45struct gdtr kgdtr;
46
47
48struct gdtdesc kgdt[GDTSIZE] = { {0, 0, 0, 0, 0, 0, 0} };
49
50unsigned int kgdtptr = 1;
51
52void init_code_desc(u32 base, u32 limite, struct gdtdesc *desc);
53void init_data_desc(u32 base, u32 limite, struct gdtdesc *desc);
54void add_gdt_desc(struct gdtdesc desc);
55void init_gdt(void);
56
57
58
59void init_gdt_desc(u32 base, u32 limite, u8 acces, u8 other,
60                   struct gdtdesc *desc)
61{
62    desc->lim0_15 = (limite & 0xffff);
63    desc->base0_15 = (base & 0xffff);
64    desc->base16_23 = (base & 0xff0000) >> 16;
65    desc->acces = acces;
66    desc->lim16_19 = (limite & 0xf0000) >> 16;
67    desc->other = (other & 0xf);
68    desc->base24_31 = (base & 0xff000000) >> 24;
69    return;
70}
71
72
73void init_code_desc(u32 base, u32 limite, struct gdtdesc *desc)
74{
75    init_gdt_desc(base, limite, 0x9B, 0x0D, desc);
76}
77
78void init_data_desc(u32 base, u32 limite, struct gdtdesc *desc)
79{
80    init_gdt_desc(base, limite, 0x93, 0x0D, desc);
81}
82
83void add_gdt_desc(struct gdtdesc desc)
84{
85    kgdt[kgdtptr] = desc;
86    kgdtptr++;
87}
88
89void init_gdt(void)
90{
91    struct gdtdesc code, data, stack;
92
93    /* initialisation des descripteurs de segment */
94    init_code_desc(0x0, 0xFFFFF, &code);
95    init_data_desc(0x0, 0xFFFFF, &data);
96    init_gdt_desc(0, 0x10, 0x97, 0x0D, &stack);
97    add_gdt_desc(code);
98    add_gdt_desc(data);
99    add_gdt_desc(stack);
100
101    /* initialisation de la structure pour GDTR */
102    kgdtr.limite = GDTSIZE * 8;
103    kgdtr.base = GDTBASE;
104
105    /* recopie de la GDT a son adresse */
106    memcpy((void *)kgdtr.base, kgdt, kgdtr.limite);
107
108    /* chargement du registre GDTR */
109    asm("lgdtl (kgdtr)");
110
111    /* initialisation des segments */
112    asm("       movw $0x10,%ax  \n \
113                movw %ax, %ds   \n \
114                movw %ax, %es   \n \
115                movw %ax, %fs   \n \
116                movw %ax, %gs   \n \
117                movw $0x18,%ax  \n \
118                movw %ax, %ss   \n \
119                movl $0x1FFFF,%esp      \n \
120                nop     \n \
121                nop     \n \
122                ljmp $0x08,$next        \n \
123                next:   \n");
124}
Note: See TracBrowser for help on using the repository browser.