backported fixes from 256boss
[bootcensus] / src / startup.s
index ef3cd38..01090cd 100644 (file)
@@ -1,5 +1,5 @@
 # pcboot - bootable PC demo/game kernel
-# Copyright (C) 2018  John Tsiombikas <nuclear@member.fsf.org>
+# Copyright (C) 2018-2019  John Tsiombikas <nuclear@member.fsf.org>
 # 
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
        .extern wait_vsync
        .extern kb_getkey
 
+       .equ STACKTOP,0x80000
+
        # move the stack to the top of the conventional memory
        cli
-       movl $0x80000, %esp
+       movl $STACKTOP, %esp
+       # push a 0 ret-addr to terminate gdb backtraces
+       pushl $0
 
        # zero the BSS section
        xor %eax, %eax
@@ -43,6 +47,34 @@ skip_bss_zero:
        hlt
        jmp 0b
 
+       # this is called once after memory init, to move the protected mode
+       # stack to the top of usable memory, to avoid interference from 16bit
+       # programs (as much as possible)
+       .global move_stack
+move_stack:
+       # calculate the currently used lowest address of the stack (rounded
+       # down to 4-byte alignment), to see where to start copying
+       mov %esp, %esi
+       and $0xfffffffc, %esi
+       # calculate the size we need to copy
+       mov $STACKTOP, %ecx
+       sub %esi, %ecx
+       # load the destination address to edi
+       mov 4(%esp), %edi
+       sub %ecx, %edi
+       # size in longwords
+       shr $2, %ecx
+
+       # change esp to the new stack
+       mov $STACKTOP, %ecx
+       sub %esp, %ecx
+       mov 4(%esp), %eax
+       mov %eax, %esp
+       sub %ecx, %esp
+
+       rep movsd
+       ret
+
        .global logohack
 logohack:
        pusha