Update Intel architecture guide references to latest.
[pintos-anon] / src / threads / loader.S
index 42f489979dff15704d4cd3efa023c9954f03913b..2d76763cffc3dcdc2aa339404bfb674360e22ae1 100644 (file)
@@ -39,7 +39,6 @@
  */
 
 #include "threads/loader.h"
-/*#include "threads/mmu.h"*/
        
 #### Kernel loader.
 
@@ -51,7 +50,7 @@
 #### memory, and jumps to the first byte of the kernel, where start.S
 #### is linked.
        
-/* Flags in control register 0 */
+/* Flags in control register 0. */
 #define CR0_PE 0x00000001      /* Protection Enable. */
 #define CR0_EM 0x00000004      /* (Floating-point) Emulation. */
 #define CR0_PG 0x80000000      /* Paging. */
@@ -69,15 +68,16 @@ start:
        cli
        cld
 
-# Set up data segments and stack.
+# Set up data segments.
 
        subw %ax, %ax
        movw %ax, %es
        movw %ax, %ds
 
+# Set up stack segment.
 # Stack grows downward starting from us.
-# We don't ever use the stack so this is strictly speaking
-# unnecessary.
+# We don't ever use the stack, but we call into the BIOS,
+# which might.
 
        movw %ax, %ss
        movw $0x7c00, %sp
@@ -111,20 +111,24 @@ start:
 #### Get memory size, via interrupt 15h function 88h.  Returns CF
 #### clear if successful, with AX = (kB of physical memory) - 1024.
 #### This only works for memory sizes <= 65 MB, which should be fine
-#### for our purposes.
+#### for our purposes.  We cap memory at 64 MB because that's all we
+#### prepare page tables for, below.
        
        movb $0x88, %ah
        int $0x15
-       jc panic                # Carry flag set on error
-       addl $1024, %eax        # Total kB
-       shrl $2, %eax           # Total 4 kB pages
-       movl %eax, ram_pages
+       jc panic
+       cli                     # BIOS might have enabled interrupts
+       addl $1024, %eax        # Total kB memory
+       cmp $0x10000, %eax      # Cap at 64 MB
+       jbe 1f
+       mov $0x10000, %eax
+1:     shrl $2, %eax           # Total 4 kB pages
+       movl %eax, ram_pgs
        
 #### Create temporary page directory and page table and set page
 #### directory base register.
 
 # Create page directory at 64 kB and fill with zeroes.
-
        mov $0x1000, %ax
        mov %ax, %es
        subl %eax, %eax
@@ -132,19 +136,32 @@ start:
        movl $0x400, %ecx
        rep stosl
 
-# Set PDEs for 0 and LOADER_PHYS_BASE to point to the page table.
-# See comments near the PG_* macros in paging.h for a description of
-# the values stored here.
+# Add PDEs to point to PTEs for the first 64 MB of RAM.
+# Also add identical PDEs starting at LOADER_PHYS_BASE.
+# See [IA32-v3a] section 3.7.6 "Page-Directory and Page-Table Entries"
+# for a description of the bits in %eax.
+       
 
        movl $0x11007, %eax
-       movl %eax, %es:0
-       movl %eax, %es:LOADER_PHYS_BASE >> 20
+       movl $0x11, %ecx
+       subl %edi, %edi
+1:     movl %eax, %es:(%di)
+       movl %eax, %es:LOADER_PHYS_BASE >> 20(%di)
+       addw $4, %di
+       addl $0x1000, %eax
+       loop 1b
 
-# Initialize page table.
+# Set up one-to-map linear to physical map for the first 64 MB of RAM.
+# See [IA32-v3a] section 3.7.6 "Page-Directory and Page-Table Entries"
+# for a description of the bits in %eax.
 
-       movl $7, %eax
-       movl $0x400, %ecx
-1:     stosl
+       movw $0x1100, %ax
+       movw %ax, %es
+       movl $0x7, %eax
+       movl $0x4000, %ecx
+       subl %edi, %edi
+1:     movl %eax, %es:(%di)
+       addw $4, %di
        addl $0x1000, %eax
        loop 1b
 
@@ -155,11 +172,9 @@ start:
        
 #### Switch to protected mode.
 
-# First we turn off interrupts because we don't set up an IDT.
+# Note that interrupts are still off.
 
-       cli
-
-# Then we point the GDTR to our GDT.  Protected mode requires a GDT.
+# Point the GDTR to our GDT.  Protected mode requires a GDT.
 # We need a data32 prefix to ensure that all 32 bits of the GDT
 # descriptor are loaded (default is to load only 24 bits).
 
@@ -198,7 +213,7 @@ start:
        movw %ax, %fs           
        movw %ax, %gs           
        movw %ax, %ss
-       movl $LOADER_PHYS_BASE + 0x20000, %esp
+       movl $LOADER_PHYS_BASE + 0x30000, %esp
 
 #### Load kernel starting at physical address LOADER_KERN_BASE by
 #### frobbing the IDE controller directly.
@@ -283,10 +298,10 @@ gdtdesc:
        .long   gdt + LOADER_PHYS_BASE  # address gdt
 
 #### Fatal error.
-#### Print panicmsg (with help from the BIOS) and spin.
+#### Print panic_message (with help from the BIOS) and spin.
 
 panic:  .code16                        # We only panic in real mode.
-       movw $panicmsg, %si
+       movw $panic_message, %si
        movb $0xe, %ah
        subb %bh, %bh
 1:     lodsb
@@ -295,22 +310,27 @@ panic:  .code16                   # We only panic in real mode.
        int $0x10
        jmp 1b
 
-panicmsg:
-       .ascii "Loader panic!\r\n"
+panic_message:
+       .ascii "Panic!"
        .byte 0
 
-#### Memory size in 4 kB pages.
-       .org LOADER_RAM_PAGES - LOADER_BASE
-ram_pages:
+#### Physical memory size in 4 kB pages.
+#### This is initialized by the loader and read by the kernel.
+       .org LOADER_RAM_PGS - LOADER_BASE
+ram_pgs:
        .long 0
 
-#### Command-line arguments inserted by another utility.
-#### The loader doesn't use these, but we note their
-#### location here for easy reference.
-       .org LOADER_CMD_LINE - LOADER_BASE
-cmd_line:
+#### Command-line arguments and their count.
+#### This is written by the `pintos' utility and read by the kernel.
+#### The loader itself does not do anything with the command line.
+       .org LOADER_ARG_CNT - LOADER_BASE
+arg_cnt:
+       .long 0
+       .org LOADER_ARGS - LOADER_BASE
+args:
        .fill 0x80, 1, 0
 
-#### Boot-sector signature for BIOS inspection.
-       .org LOADER_BIOS_SIG - LOADER_BASE
+#### Boot-sector signature.
+#### The BIOS checks that this is set properly.
+       .org LOADER_SIG - LOADER_BASE
        .word 0xaa55