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

Last change on this file since 4333 was 4333, checked in by Sam Hocevar, 10 years ago

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

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