Skip to content

Commit 512443e

Browse files
committed
kernel: Experimental move stack to 640k EMA boundary
1 parent b1dd9a5 commit 512443e

4 files changed

Lines changed: 58 additions & 82 deletions

File tree

src/boot/bootloader.asm

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
;; Licensed under the Apache License, Version 2.0 (the "License");
77
;; you may not use this file except in compliance with the License.
88
;; You may obtain a copy of the License at
9-
;;
9+
;;
1010
;; http:;;www.apache.org/licenses/LICENSE-2.0
11-
;;
11+
;;
1212
;; Unless required by applicable law or agreed to in writing, software
1313
;; distributed under the License is distributed on an "AS IS" BASIS,
1414
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,16 +17,16 @@
1717

1818
USE16
1919
;; Memory layout, 16-bit
20-
%define _boot_segment 0x07c0
20+
%define _boot_segment 0x07c0
2121
%define _vga_segment 0xb800
2222

2323
;; Memory layout, 32-bit
2424
%define _mode32_code_segment 0x08
2525
%define _mode32_data_segment 0x10
26-
27-
%define _kernel_loc 0x200000
28-
%define _kernel_stack 0x200000
29-
26+
27+
%define _kernel_loc 0x100000
28+
%define _kernel_stack 0xA0000
29+
3030
;; We don't really need a stack, except for calls
3131
%define _stack_segment 0x7000
3232
%define _stack_pointer 0xfffe ;Within the ss, for 16 bit
@@ -37,13 +37,13 @@ USE16
3737

3838
; ELF offset of _start in text section
3939
%define _elf_start 0x34
40-
40+
4141
;;; START
4242
;;; We'll start at the beginning, but jump over some data
4343
_start:
4444
jmp boot
4545

46-
;;; The size of the service on disk, to be loaded at boot. (Convenient to
46+
;;; The size of the service on disk, to be loaded at boot. (Convenient to
4747
;;; have it at the beginning, so it remains at a fixed location. )
4848
srv_size:
4949
dd 0
@@ -53,10 +53,10 @@ srv_offs:
5353
;;; Actual start
5454
boot:
5555
;; Need to set data segment, to access strings
56-
mov ax, _boot_segment
57-
mov ds, ax
58-
59-
; fast a20 enable
56+
mov ax, _boot_segment
57+
mov ds, ax
58+
59+
; fast a20 enable
6060
in al, 0x92
6161
or al, 2
6262
out 0x92, al
@@ -71,19 +71,19 @@ boot:
7171
mov [color], ax
7272
mov esi, str_literally
7373
call printstr
74-
75-
74+
75+
7676
; check that it was enabled
7777
test al,0
78-
jz .a20_ok
78+
jz .a20_ok
7979
;; NOT OK
8080
mov esi,str_a20_fail
81-
call printstr
81+
call printstr
8282
cli
8383
hlt
8484
.a20_ok:
85-
call protected_mode
86-
85+
call protected_mode
86+
8787
protected_mode:
8888
;xchg bx,bx
8989
cli
@@ -100,16 +100,16 @@ fill_screen:
100100
mov es, bx
101101
mov bx, 0
102102
mov al, 0
103-
mov ah, [color]
104-
.fill:
103+
mov ah, [color]
104+
.fill:
105105
mov [es:bx], ax
106106
add bx,2
107107
cmp bx, (25*80*2)
108108
jge .done
109109
jmp .fill
110110
.done:
111111
ret
112-
112+
113113
printstr:
114114
mov bx, _vga_segment
115115
mov es, bx
@@ -126,18 +126,18 @@ printstr:
126126
.done:
127127
mov [cursor], bx
128128
ret
129-
129+
130130
str_includeos:
131131
db `#include <os> `,0
132-
str_literally:
132+
str_literally:
133133
db `\/\/ Literally `,0
134-
str_a20_fail:
134+
str_a20_fail:
135135
db `A20 Err\n\r`,0
136-
cursor:
136+
cursor:
137137
dw (80 * 11 * 2) + 48
138138
color:
139139
dw 0x0d
140-
140+
141141
USE32
142142
ALIGN 32
143143
;; Global descriptor table
@@ -146,7 +146,7 @@ gdtr:
146146
dq gdt32+(_boot_segment<<4)
147147
gdt32:
148148
;; Entry 0x0: Null desriptor
149-
dq 0x0
149+
dq 0x0
150150
;; Entry 0x8: Code segment
151151
dw 0xffff ;Limit
152152
dw 0x0000 ;Base 15:00
@@ -158,68 +158,68 @@ gdt32:
158158
dw 0x0000 ;Base 15:00
159159
db 0x00 ;Base 23:16
160160
dw 0xcf92 ;Flags
161-
db 0x00 ;Base 32:24
161+
db 0x00 ;Base 32:24
162162
gdt32_end:
163163
db `32`
164164
;;; GDT done
165-
165+
166166
;;; 32-bit code
167-
USE32
167+
USE32
168168
ALIGN 32
169169
mode32:
170-
;; Set up 32-bit data segment
170+
;; Set up 32-bit data segment
171171
mov eax,_mode32_data_segment
172172
;; Set up stack
173173
mov ss,eax
174174
mov esp,_kernel_stack
175175
;; mov esi,_kernel_stack ;Was e00c3
176176
mov ebp,_kernel_stack
177-
177+
178178
;; Set up data segment
179179
mov ds, eax
180180
mov es, eax
181181
mov fs, eax
182182
mov gs, eax
183-
184-
;; By default QEMU handles only 1 sector pr. read
185-
%define LOAD_SIZE 1
186-
183+
184+
;; By default QEMU handles only 1 sector pr. read
185+
%define LOAD_SIZE 1
186+
187187
;; Number of sectors to read for the entire service
188188
mov edx, [srv_size+(_boot_segment<<4)]
189189
mov eax, 1
190-
190+
191191
;; Location to load kernel
192192
mov edi,_kernel_loc
193-
193+
194194
.more:
195195
mov cl, LOAD_SIZE
196-
196+
197197
;; Load 1 sector from disk
198198
call ata_lba_read
199-
199+
200200
;; Increase LBA by 1 sector
201201
add eax, LOAD_SIZE
202-
202+
203203
;; Increase destination (in bytes)
204204
add edi, LOAD_SIZE * 512
205-
205+
206206
;; Decrement counter (srv_size) by 1 sector
207207
sub edx, LOAD_SIZE
208-
208+
209209
;; If all sectors loaded, move on, else get .more
210210
cmp edx, 0
211211
jge .more ;; jump when gequal
212-
212+
213213
;; Bochs breakpoint
214214
;;xchg bx,bx
215-
215+
216216
;; GERONIMO!
217217
;; Jump to service
218218
call DWORD [srv_offs+(_boot_segment<<4)]
219-
220-
219+
220+
221221
%include "boot/disk_read_lba.asm"
222-
222+
223223
;; BOOT SIGNATURE
224224
times 510-($-$$) db 0 ;
225225
dw 0xAA55

src/kernel/os.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ extern "C" uintptr_t get_cpu_esp();
7171

7272
void OS::start(uint32_t boot_magic, uint32_t boot_addr) {
7373

74+
atexit(default_exit);
7475
default_stdout_handlers();
7576

7677
// Print a fancy header
@@ -80,7 +81,7 @@ void OS::start(uint32_t boot_magic, uint32_t boot_addr) {
8081

8182
auto esp = get_cpu_esp();
8283
MYINFO ("Stack: 0x%x", esp);
83-
Expects (esp < (uintptr_t)&_LOAD_START_ and esp >= 0x100000 and "Stack location OK");
84+
Expects (esp < 0xA0000 and esp > 0x0 and "Stack location OK");
8485

8586
MYINFO("Boot args: 0x%x (multiboot magic), 0x%x (bootinfo addr)",
8687
boot_magic, boot_addr);
@@ -107,9 +108,6 @@ void OS::start(uint32_t boot_magic, uint32_t boot_addr) {
107108
}
108109
}
109110

110-
// ?
111-
atexit(default_exit);
112-
113111
MYINFO("Assigning fixed memory ranges (Memory map)");
114112
auto& memmap = memory_map();
115113

@@ -118,8 +116,8 @@ void OS::start(uint32_t boot_magic, uint32_t boot_addr) {
118116
"EBDA", "Extended BIOS data area"});
119117
memmap.assign_range({0x000A0000, 0x000FFFFF,
120118
"VGA/ROM", "Memory mapped video memory"});
121-
memmap.assign_range({0x00100000, (uintptr_t)&_LOAD_START_ -1 ,
122-
"Stack", "Kernel / service main stack"});
119+
//memmap.assign_range({0x00100000, (uintptr_t)&_LOAD_START_ -1 ,
120+
// "Stack", "Kernel / service main stack"});
123121
memmap.assign_range({(uintptr_t)&_LOAD_START_, (uintptr_t)&_end,
124122
"ELF", "Your service binary including OS"});
125123

src/kernel/start.asm

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,17 @@
1616
;; limitations under the License.
1717

1818
USE32
19-
section .text
20-
2119
extern kernel_start
22-
extern _LOAD_START_
23-
extern __stack_rand_ba
24-
2520
global _start
2621

22+
section .text
2723
;; Multiboot places boot paramters on eax and ebx.
2824
_start:
29-
;; Stack base address
30-
mov esp, _LOAD_START_
31-
;; Primitive stack base address randomization
32-
mov ecx, __stack_rand_ba
33-
and ecx, 0xff
34-
shl ecx, 10 ;; up to 256kb per 256 seconds
35-
sub esp, ecx
36-
mov [boot_magic], eax
37-
rdtsc
38-
and eax, 0xff
39-
shl eax, 6 ;; 64 byte per tick, up to 16kb
40-
41-
;; NOTE: Stack changes here (pushes before this point won't pop right)
42-
sub esp, eax
43-
44-
;; make esp page-aligned
45-
and esp, 0xfffff000
25+
;; Stack base address to EMA boundary
26+
mov esp, 0xA0000
27+
sub esp, 0x10
4628

4729
;; Place multiboot parameters on stack
48-
mov eax, [boot_magic]
4930
push ebx
5031
push eax
5132
call kernel_start
52-
53-
boot_magic:
54-
dw 0

src/linker.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ PHDRS {
2424

2525
SECTIONS
2626
{
27-
PROVIDE ( _ELF_START_ = . + 0x200000);
27+
PROVIDE ( _ELF_START_ = . + 0x100000);
2828
PROVIDE ( _LOAD_START_ = _ELF_START_); /* For convenience w. multiboot */
2929

3030
.multiboot (_ELF_START_ + SIZEOF_HEADERS): {

0 commit comments

Comments
 (0)