/SPARTAN/trunk/arch/amd64/include/pm.h |
---|
0,0 → 1,147 |
/* |
* Copyright (C) 2001-2004 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#ifndef __PM_H__ |
#define __PM_H__ |
#include <arch/types.h> |
#include <typedefs.h> |
#include <arch/context.h> |
#define IDT_ITEMS 64 |
#define GDT_ITEMS 6 |
#define NULL_DES 0 |
#define KTEXT_DES 1 |
#define KDATA_DES 2 |
#define UTEXT_DES 3 |
#define UDATA_DES 4 |
#define TSS_DES 5 |
#define selector(des) ((des)<<3) |
#define PL_KERNEL 0 |
#define PL_USER 3 |
#define AR_PRESENT (1<<7) |
#define AR_DATA (2<<3) |
#define AR_CODE (3<<3) |
#define AR_WRITABLE (1<<1) |
#define AR_INTERRUPT (0xe) |
#define AR_TSS (0x9) |
#define DPL_KERNEL (PL_KERNEL<<5) |
#define DPL_USER (PL_USER<<5) |
#define IO_MAP_BASE (104) |
struct ptr_16_32 { |
__u16 limit; |
__u32 base; |
} __attribute__ ((packed)); |
struct descriptor { |
unsigned limit_0_15: 16; |
unsigned base_0_15: 16; |
unsigned base_16_23: 8; |
unsigned access: 8; |
unsigned limit_16_19: 4; |
unsigned available: 1; |
unsigned longmode: 1; |
unsigned special: 1; |
unsigned granularity : 1; |
unsigned base_24_31: 8; |
} __attribute__ ((packed)); |
struct idescriptor { |
unsigned offset_0_15: 16; |
unsigned selector: 16; |
unsigned unused: 8; |
unsigned access: 8; |
unsigned offset_16_31: 16; |
} __attribute__ ((packed)); |
struct tss { |
__u16 link; |
unsigned : 16; |
__u32 esp0; |
__u16 ss0; |
unsigned : 16; |
__u32 esp1; |
__u16 ss1; |
unsigned : 16; |
__u32 esp2; |
__u16 ss2; |
unsigned : 16; |
__u32 cr3; |
__u32 eip; |
__u32 eflags; |
__u32 eax; |
__u32 ecx; |
__u32 edx; |
__u32 ebx; |
__u32 esp; |
__u32 ebp; |
__u32 esi; |
__u32 edi; |
__u16 es; |
unsigned : 16; |
__u16 cs; |
unsigned : 16; |
__u16 ss; |
unsigned : 16; |
__u16 ds; |
unsigned : 16; |
__u16 fs; |
unsigned : 16; |
__u16 gs; |
unsigned : 16; |
__u16 ldtr; |
unsigned : 16; |
unsigned : 16; |
__u16 io_map_base; |
} __attribute__ ((packed)); |
extern struct ptr_16_32 gdtr; |
extern struct tss *tss_p; |
extern struct descriptor gdt[]; |
extern struct idescriptor idt[]; |
extern void pm_init(void); |
extern void gdt_setbase(struct descriptor *d, __address base); |
extern void gdt_setlimit(struct descriptor *d, __u32 limit); |
extern void idt_init(void); |
extern void idt_setoffset(struct idescriptor *d, __address offset); |
extern void tss_initialize(struct tss *t); |
#endif |
/SPARTAN/trunk/arch/amd64/include/boot/memmap.h |
---|
0,0 → 1,48 |
/* |
* Copyright (C) 2005 Josef Cejka |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#ifndef __ia32_MEMMAP_H__ |
#define __ia32_MEMMAP_H__ |
#include <arch/boot/memmapasm.h> |
#include <arch/types.h> |
struct e820memmap_ { |
__u64 base_address; |
__u64 size; |
__u32 type; |
} __attribute__ ((packed)); |
extern struct e820memmap_ e820table[MEMMAP_E820_MAX_RECORDS]; |
extern __u8 e820counter; |
extern __u32 e801memorysize; // size of memory in KB |
#endif |
/SPARTAN/trunk/arch/amd64/include/boot/memmapasm.h |
---|
0,0 → 1,50 |
/* |
* Copyright (C) 2005 Josef Cejka |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#ifndef __ia32_MEMMAPASM_H__ |
#define __ia32_MEMMAPASM_H__ |
/* E820h memory range types - other values*/ |
/* Free memory */ |
#define MEMMAP_MEMORY_AVAILABLE 1 |
/* Not available for OS */ |
#define MEMMAP_MEMORY_RESERVED 2 |
/* OS may use it after reading ACPI table */ |
#define MEMMAP_MEMORY_ACPI 3 |
/* Unusable, required to be saved and restored across an NVS sleep */ |
#define MEMMAP_MEMORY_NVS 4 |
/* Corrupted memory */ |
#define MEMMAP_MEMORY_UNUSABLE 5 |
/* size of one entry */ |
#define MEMMAP_E820_RECORD_SIZE 20 |
/* maximum entries */ |
#define MEMMAP_E820_MAX_RECORDS 32 |
#endif |
/SPARTAN/trunk/arch/amd64/Makefile.inc |
---|
16,4 → 16,6 |
arch_sources = arch/dummy.s \ |
arch/fpu_context.c \ |
arch/putchar.c \ |
arch/boot/boot.S |
arch/boot/boot.S \ |
arch/boot/memmap.S \ |
arch/pm.c |
/SPARTAN/trunk/arch/amd64/src/boot/boot.S |
---|
39,13 → 39,55 |
# switch to protected mode. |
# |
kernel_image_start: |
cli |
xorw %ax,%ax |
movw %ax,%ds |
movw %ax,%ss # initialize stack segment register |
movl $0x7c00,%esp # initialize stack pointer |
meeting_point: |
.code32 |
call memmap_arch_init |
mov $0x80000000, %eax |
cpuid |
cmp $0x80000000, %eax # any function > 80000000h? |
jbe no_long_mode |
mov $0x80000001, %eax # Extended function code 80000001 |
cpuid |
bt $29, %edx # Test if long mode is supported. |
jnc no_long_mode |
# Fill out GDTR.base, IDTR.base |
leal gdtr, %eax |
movl gdt_addr, %ebx |
movl %ebx, 2(%eax) |
movl idt_addr, %ebx |
leal idtr, %eax |
movl %ebx, 2(%eax) |
# Load gdtr, idtr |
lgdt gdtr |
lidt idtr |
mov $1, %eax # Enable protected mode (CR0.PE = 1) |
mov %eax, %cr0 |
jmpl $8, $now_in_prot |
now_in_prot: |
no_long_mode: |
1: |
jmp 1b |
.section K_DATA_START |
.align 4096 |
page_directory: |
.space 4096, 0 |
gdt_addr: |
.quad gdt + 0x80000000 |
idt_addr: |
.quad idt + 0x80000000 |
/SPARTAN/trunk/arch/amd64/src/boot/memmap.S |
---|
0,0 → 1,125 |
/* |
* Copyright (C) 2005 Josef Cejka |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#include <arch/boot/memmapasm.h> |
E820_RECORD_SIZE = MEMMAP_E820_RECORD_SIZE |
E820_MAX_RECORDS = MEMMAP_E820_MAX_RECORDS |
E820_SMAP = 0x534d4150 |
.global memmap_arch_init |
.global e820counter |
.global e820table |
.global e801memorysize |
.code16 |
.section K_TEXT_START_2 |
memmap_arch_init: |
e820begin: |
xorl %ebx,%ebx # during first call, ebx must be 0 |
movw %bx,%ds |
movw %bx,%es |
movw $e820table,%di |
movb $E820_MAX_RECORDS,e820counter |
e820loop: |
movl $E820_SMAP,%edx # control sequence "SMAP" |
movl $0x0000e820,%eax # service |
movl $E820_RECORD_SIZE,%ecx |
int $0x15 |
jc e820err |
cmpl $E820_SMAP,%eax # verifying BIOS |
jne e820err |
cmpl $E820_RECORD_SIZE,%ecx |
jne e820err # bad record size - bug in bios |
movw %di,%ax # next record |
addw $E820_RECORD_SIZE,%ax |
movw %ax,%di |
decb e820counter # buffer is full |
jz e820end |
cmpl $0,%ebx |
jne e820loop |
e820end: |
movb $E820_MAX_RECORDS,%al |
subb e820counter,%al |
movb %al,e820counter # store # of valid entries in e820counter |
jmp e801begin |
e820err: |
movb $0,e820counter |
# method e801 - get size of memory |
e801begin: |
xorw %dx,%dx |
xorw %cx,%cx |
xorw %bx,%bx |
movw $0xe801,%ax |
stc |
int $0x15 |
jc e801end |
# fix problem with some BIOSes which use ax:bx rather than cx:dx |
testw %cx,%cx |
jnz e801cxdx |
testw %dx,%dx |
jnz e801cxdx |
movw %ax,%cx |
movw %bx,%dx |
e801cxdx: |
andl $0xffff,%edx |
shll $6,%edx |
andl $0xffff,%ecx |
addl %ecx,%edx |
addl $0x0400,%edx # add lower 1 MB - it's not included by e801 method |
movl %edx,e801memorysize |
e801end: |
ret |
#memory size in 1 kb chunks |
e801memorysize: |
.long 0 |
e820counter: |
.byte 0xff |
e820table: |
.space (32*E820_RECORD_SIZE),0xff # space for 32 records, each E820_RECORD_SIZE bytes long |
/SPARTAN/trunk/arch/amd64/src/pm.c |
---|
0,0 → 1,98 |
/* |
* Copyright (C) 2001-2004 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#include <arch/pm.h> |
#include <arch/mm/page.h> |
#include <arch/types.h> |
/* |
* There is no segmentation in long mode so we set up flat mode. In this |
* mode, we use, for each privilege level, two segments spanning the |
* whole memory. One is for code and one is for data. |
*/ |
struct descriptor gdt[GDT_ITEMS] = { |
/* NULL descriptor */ |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
/* KTEXT descriptor */ |
{ .limit_0_15 = 0xffff, |
.base_0_15 = 0, |
.base_16_23 = 0, |
.access = AR_PRESENT | AR_CODE | DPL_KERNEL, |
.limit_16_19 = 0xf, |
.available = 0, |
.longmode = 1, |
.special = 0, |
.granularity = 1, |
.base_24_31 = 0 }, |
/* KDATA descriptor */ |
{ .limit_0_15 = 0xffff, |
.base_0_15 = 0, |
.base_16_23 = 0, |
.access = AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_KERNEL, |
.limit_16_19 = 0xf, |
.available = 0, |
.longmode = 0, |
.special = 0, |
.granularity = 0, |
.base_24_31 = 0 }, |
/* UTEXT descriptor */ |
{ .limit_0_15 = 0xffff, |
.base_0_15 = 0, |
.base_16_23 = 0, |
.access = AR_PRESENT | AR_CODE | DPL_USER, |
.limit_16_19 = 0xf, |
.available = 0, |
.longmode = 1, |
.special = 0, |
.granularity = 0, |
.base_24_31 = 0 }, |
/* UDATA descriptor */ |
{ .limit_0_15 = 0xffff, |
.base_0_15 = 0, |
.base_16_23 = 0, |
.access = AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_USER, |
.limit_16_19 = 0xf, |
.available = 0, |
.longmode = 0, |
.special = 1, |
.granularity = 1, |
.base_24_31 = 0 }, |
/* TSS descriptor - set up will be completed later */ |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } |
}; |
struct idescriptor idt[IDT_ITEMS]; |
static struct tss tss; |
/* gdtr is changed by kmp before next CPU is initialized */ |
struct ptr_16_32 gdtr __attribute__ ((section ("K_DATA_START"))) = { .limit = sizeof(gdt) }; |
//struct ptr_16_32 gdtr __attribute__ ((section ("K_DATA_START"))) = { .limit = sizeof(gdt), .base = KA2PA((__address) gdt) }; |
struct ptr_16_32 idtr __attribute__ ((section ("K_DATA_START"))) = { .limit = sizeof(idt) }; |
/SPARTAN/trunk/arch/amd64/_link.ld |
---|
1,4 → 1,4 |
/** IA-32 linker script |
/** AMD64 linker script |
* |
* umapped section: |
* kernel text |
22,7 → 22,7 |
unmapped_kdata_end = .; |
} |
.mapped (-0x80000000+SIZEOF(.unmapped)) : AT (0x8000+SIZEOF(.unmapped)) { |
.mapped (0xffffffff80000000+SIZEOF(.unmapped)+0x8000) : AT (0x8000+SIZEOF(.unmapped)) { |
ktext_start = .; |
*(.text); |
ktext_end = .; |
32,6 → 32,7 |
*(.rodata*); /* string literals */ |
*(COMMON); /* global variables */ |
*(.bss); /* uninitialized static variables */ |
*(.eh_frame); |
*(K_DATA_END); |
kdata_end = .; |
} |