Commit 28096216 authored by Ian Seyler's avatar Ian Seyler

Optimizations (xor eax, eax vs xor rax, rax) and it's 2018!

parent e7e94a70
......@@ -18,7 +18,7 @@
%define DAP_SEGMENT 0x0000
USE16
BITS 16
org 0x7C00
entry:
......
; =============================================================================
; Pure64 Multiboot -- a 64-bit OS/software loader written in Assembly for x86-64 systems
; Copyright (C) 2008-2017 Return Infinity -- see LICENSE.TXT
; Copyright (C) 2008-2018 Return Infinity -- see LICENSE.TXT
;
; http://stackoverflow.com/questions/33488194/creating-a-simple-multiboot-kernel-loaded-with-grub2
; https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#OS-image-format
......
; =============================================================================
; Pure64 Multiboot 2 -- a 64-bit OS/software loader written in Assembly for x86-64 systems
; Copyright (C) 2008-2017 Return Infinity -- see LICENSE.TXT
; Copyright (C) 2008-2018 Return Infinity -- see LICENSE.TXT
;
; http://nongnu.askapache.com/grub/phcoder/multiboot.pdf
; =============================================================================
......
......@@ -7,7 +7,7 @@ This manual has the purpose of helping software developers understand how they s
This manual assumes you have some understanding of a typical boot process.
You'll have to have GCC and NASM installed to build the software and Doxygen for the documentation.
You'll have to have NASM installed to build the software.
## Building
......@@ -15,8 +15,7 @@ You'll have to have GCC and NASM installed to build the software and Doxygen for
In the top directory of Pure64, just run the following:
```
make
sudo make install
./build.sh
```
......@@ -36,6 +35,7 @@ Once it's loaded, it enters an infinite loop.
The file can be called `kernel.asm`.
```
BITS 64
ORG 0x100000
start:
......@@ -73,49 +73,6 @@ gcc kernel.c -o kernel -mno-red-zone -fno-stack-protector -fomit-frame-pointer
The flags added to the command are there to help GCC produce could that will run in kernel space.
## Creating a Disk Image
Once Pure64 is installed, you'll have to create the configuration file for the disk image.
Here's a very simple one:
arch: x86_64
bootsector: mbr
partition_scheme: gpt
It creates a disk image for x86_64 systems, using MBR bootsector, formatted with a GPT partition table.
Name the file `pure64-config.txt` for now.
You can now do this to create the image.
```
pure64 init
pure64 cp my-kernel.bin /boot/kernel
```
Here's a breakdown of what these commands are doing.
- `init` reads `pure64-config.txt` and creates `pure64.img` based on the variables in there.
- `cp` copies a file from the host system to the disk image. In this case, it copies the kernel.
When the kernel is loaded, it is loaded at the address `0x100000`. They entry point must be at the beginning of the binary.
The ELF file format is also supported, just as long as it is found at `/boot/kernel`.
If the kernel is formatted with ELF, then the entry point is defined by the ELF file and the load address is specified by the program headers.
The load address in the ELF file should be at least `0x100000`.
## Running the Disk Image with QEMU
To run the disk image with qemu, enter the Pure64 directory and run `test.sh`.
Like this:
```
pure64 init
pure64 cp my-kernel.bin /boot/kernel
./test.sh
```
## Memory Map
This memory map shows how physical memory looks after Pure64 is finished.
......
; =============================================================================
; Pure64 -- a 64-bit OS/software loader written in Assembly for x86-64 systems
; Copyright (C) 2008-2017 Return Infinity -- see LICENSE.TXT
; Copyright (C) 2008-2018 Return Infinity -- see LICENSE.TXT
;
; INIT ACPI
; =============================================================================
init_acpi:
mov rsi, 0x00000000000E0000 ; Start looking for the Root System Description Pointer Structure
mov esi, 0x000E0000 ; Start looking for the Root System Description Pointer Structure
mov rbx, 'RSD PTR ' ; This in the Signature for the ACPI Structure Table (0x2052545020445352)
searchingforACPI:
lodsq ; Load a quad word from RSI and store in RAX, then increment RSI by 8
cmp rax, rbx
je foundACPI
cmp rsi, 0x00000000000FFFFF ; Keep looking until we get here
cmp esi, 0x000FFFFF ; Keep looking until we get here
jge noACPI ; ACPI tables couldn't be found, Fail.
jmp searchingforACPI
......
; =============================================================================
; Pure64 -- a 64-bit OS/software loader written in Assembly for x86-64 systems
; Copyright (C) 2008-2017 Return Infinity -- see LICENSE.TXT
; Copyright (C) 2008-2018 Return Infinity -- see LICENSE.TXT
;
; INIT CPU - This code is called by all activated CPU cores in the system
; =============================================================================
......
; =============================================================================
; Pure64 -- a 64-bit OS/software loader written in Assembly for x86-64 systems
; Copyright (C) 2008-2017 Return Infinity -- see LICENSE.TXT
; Copyright (C) 2008-2018 Return Infinity -- see LICENSE.TXT
;
; INIT PIC
; =============================================================================
......
; =============================================================================
; Pure64 -- a 64-bit OS/software loader written in Assembly for x86-64 systems
; Copyright (C) 2008-2017 Return Infinity -- see LICENSE.TXT
; Copyright (C) 2008-2018 Return Infinity -- see LICENSE.TXT
;
; INIT SMP
; =============================================================================
......
; =============================================================================
; Pure64 -- a 64-bit OS/software loader written in Assembly for x86-64 systems
; Copyright (C) 2008-2017 Return Infinity -- see LICENSE.TXT
; Copyright (C) 2008-2018 Return Infinity -- see LICENSE.TXT
;
; INIT SMP AP
; =============================================================================
USE16
BITS 16
init_smp_ap:
jmp 0x0000:clearcs_ap
......@@ -41,7 +41,7 @@ align 16
; =============================================================================
; 32-bit mode
USE32
BITS 32
startap32:
mov eax, 16 ; load 4 GB data descriptor
......@@ -90,17 +90,17 @@ align 16
; =============================================================================
; 64-bit mode
USE64
BITS 64
startap64:
xor rax, rax ; aka r0
xor rbx, rbx ; aka r3
xor rcx, rcx ; aka r1
xor rdx, rdx ; aka r2
xor rsi, rsi ; aka r6
xor rdi, rdi ; aka r7
xor rbp, rbp ; aka r5
xor rsp, rsp ; aka r4
xor eax, eax ; aka r0
xor ebx, ebx ; aka r3
xor ecx, ecx ; aka r1
xor edx, edx ; aka r2
xor esi, esi ; aka r6
xor edi, edi ; aka r7
xor ebp, ebp ; aka r5
xor esp, esp ; aka r4
xor r8, r8
xor r9, r9
xor r10, r10
......@@ -120,7 +120,7 @@ startap64:
jmp rax
nop
clearcs64_ap:
xor rax, rax
xor eax, eax
; Reset the stack. Each CPU gets a 1024-byte unique stack location
mov rsi, [os_LocalAPICAddress] ; We would call os_smp_get_id here but the stack is not ...
......
; =============================================================================
; Pure64 -- a 64-bit OS/software loader written in Assembly for x86-64 systems
; Copyright (C) 2008-2017 Return Infinity -- see LICENSE.TXT
; Copyright (C) 2008-2018 Return Infinity -- see LICENSE.TXT
;
; Interrupts
; =============================================================================
......
; =============================================================================
; Pure64 -- a 64-bit OS/software loader written in Assembly for x86-64 systems
; Copyright (C) 2008-2017 Return Infinity -- see LICENSE.TXT
; Copyright (C) 2008-2018 Return Infinity -- see LICENSE.TXT
;
; The first stage loader is required to gather information about the system
; while the BIOS or UEFI is still available and load the Pure64 binary to
......@@ -16,14 +16,12 @@
; Max size of the resulting pure64.sys is 32768 bytes (32KiB)
; =============================================================================
USE32
BITS 32
ORG 0x8000
PURE64SIZE equ 4096 ; Pad Pure64 to this length
STAGE3 equ 0x60000 ; Stage three bootloader is at this address.
start:
jmp start32 ; This command will be overwritten with 'NOP's before the AP's are started
nop
......@@ -31,7 +29,7 @@ start:
; =============================================================================
; Code for AP startup
USE16
BITS 16
cli ; Disable all interrupts
xor eax, eax
xor ebx, ebx
......@@ -51,7 +49,7 @@ USE16
; =============================================================================
; 32-bit mode
USE32
BITS 32
start32:
mov eax, 16 ; Set the correct segment registers
mov ds, ax
......@@ -60,11 +58,6 @@ start32:
mov fs, ax
mov gs, ax
mov edi, 0xb8000 ; Clear the screen
mov ax, 0x0720
mov cx, 2000
rep stosw
mov edi, 0x5000 ; Clear the info map
xor eax, eax
mov cx, 512
......@@ -245,7 +238,7 @@ align 16
; =============================================================================
; 64-bit mode
USE64
BITS 64
start64:
xor eax, eax ; aka r0
......@@ -275,7 +268,7 @@ start64:
jmp rax ; jmp SYS64_CODE_SEL:start64 would have sent us ...
nop ; out of compatibility mode and into 64-bit mode
clearcs64:
xor rax, rax
xor eax, eax
lgdt [GDTR64] ; Reload the GDT
......@@ -333,7 +326,7 @@ make_interrupt_gates: ; make gates for the other interrupts
stosw ; store the high word (31..16) of the address
shr rax, 16
stosd ; store the extra high dword (63..32) of the address.
xor rax, rax
xor eax, eax
stosd ; reserved
dec rcx
jnz make_interrupt_gates
......@@ -361,26 +354,26 @@ make_interrupt_gates: ; make gates for the other interrupts
mov word [0x12*16], exception_gate_18
mov word [0x13*16], exception_gate_19
mov rdi, 0x21 ; Set up Keyboard handler
mov rax, keyboard
mov edi, 0x21 ; Set up Keyboard handler
mov eax, keyboard
call create_gate
mov rdi, 0x22 ; Set up Cascade handler
mov rax, cascade
mov edi, 0x22 ; Set up Cascade handler
mov eax, cascade
call create_gate
mov rdi, 0x28 ; Set up RTC handler
mov rax, rtc
mov edi, 0x28 ; Set up RTC handler
mov eax, rtc
call create_gate
lidt [IDTR64] ; load IDT register
; Clear memory 0xf000 - 0xf7ff for the infomap (2048 bytes)
xor rax, rax
mov rcx, 256
xor eax, eax
mov ecx, 256
mov rdi, 0x000000000000F000
clearmapnext:
stosq
dec rcx
cmp rcx, 0
dec ecx
cmp ecx, 0
jne clearmapnext
call init_acpi ; Find and process the ACPI tables
......@@ -389,8 +382,7 @@ clearmapnext:
call init_pic ; Configure the PIC(s), also activate interrupts
; Init of SMP
call init_smp
call init_smp ; Init of SMP
; Reset the stack to the proper location (was set to 0x8000 previously)
mov rsi, [os_LocalAPICAddress] ; We would call os_smp_get_id here but the stack is not ...
......@@ -402,7 +394,7 @@ clearmapnext:
mov rsp, rax ; Pure64 leaves 0x50000-0x9FFFF free so we use that
; Calculate amount of usable RAM from Memory Map
xor rcx, rcx
xor ecx, ecx
mov rsi, 0x0000000000006000 ; E820 Map location
readnextrecord:
lodsq
......@@ -434,7 +426,7 @@ endmemcalc:
mov dword [mem_amount], ecx
; Build the infomap
xor rdi, rdi
xor edi, edi
mov di, 0x5000
mov rax, [os_ACPITableAddress]
stosq
......@@ -483,9 +475,9 @@ nextIOAPIC:
stosb
; Move the trailing binary to its final location
mov rsi, 0x8000+PURE64SIZE ; Memory offset to end of pure64.sys
mov rdi, 0x100000 ; Destination address at the 1MiB mark
mov rcx, ((32768 - PURE64SIZE) / 8)
mov esi, 0x8000+PURE64SIZE ; Memory offset to end of pure64.sys
mov edi, 0x100000 ; Destination address at the 1MiB mark
mov ecx, ((32768 - PURE64SIZE) / 8)
rep movsq ; Copy 8 bytes at a time
; Output message via serial port
......
; =============================================================================
; Pure64 -- a 64-bit OS/software loader written in Assembly for x86-64 systems
; Copyright (C) 2008-2017 Return Infinity -- see LICENSE.TXT
; Copyright (C) 2008-2018 Return Infinity -- see LICENSE.TXT
;
; System Variables
; =============================================================================
......@@ -10,8 +10,6 @@ message: db 10, 'Pure64 OK', 10
;CONFIG
cfg_smpinit: db 1 ; By default SMP is enabled. Set to 0 to disable.
cfg_mbr: db 0 ; Did we boot off of a disk with a proper MBR
cfg_hdd: db 0 ; Was a bootable drive detected
; Memory locations
E820Map: equ 0x0000000000004000
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment