Skip to content

Commit

Permalink
spacing fixes: no tabs, 2-space indents (for rtm)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsc committed Sep 6, 2006
1 parent 45854ca commit a650c60
Show file tree
Hide file tree
Showing 33 changed files with 916 additions and 908 deletions.
27 changes: 14 additions & 13 deletions asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
// macros to create x86 segments from assembler
//

#define SEG_NULLASM \
.word 0, 0; \
.byte 0, 0, 0, 0
#define SEG_ASM(type,base,lim) \
.word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \
.byte (((base) >> 16) & 0xff), (0x90 | (type)), \
(0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff)
#define SEG_NULLASM \
.word 0, 0; \
.byte 0, 0, 0, 0

#define STA_X 0x8 // Executable segment
#define STA_E 0x4 // Expand down (non-executable segments)
#define STA_C 0x4 // Conforming code segment (executable only)
#define STA_W 0x2 // Writeable (non-executable segments)
#define STA_R 0x2 // Readable (executable segments)
#define STA_A 0x1 // Accessed
#define SEG_ASM(type,base,lim) \
.word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \
.byte (((base) >> 16) & 0xff), (0x90 | (type)), \
(0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff)

#define STA_X 0x8 // Executable segment
#define STA_E 0x4 // Expand down (non-executable segments)
#define STA_C 0x4 // Conforming code segment (executable only)
#define STA_W 0x2 // Writeable (non-executable segments)
#define STA_R 0x2 // Readable (executable segments)
#define STA_A 0x1 // Accessed
139 changes: 73 additions & 66 deletions bootasm.S
Original file line number Diff line number Diff line change
@@ -1,96 +1,103 @@
#include "asm.h"
.set PROT_MODE_CSEG,0x8 # code segment selector
.set PROT_MODE_CSEG,0x8 # code segment selector
.set PROT_MODE_DSEG,0x10 # data segment selector
.set CR0_PE_ON,0x1 # protected mode enable flag
.set CR0_PE_ON,0x1 # protected mode enable flag
###################################################################################
# ENTRY POINT
# ENTRY POINT
# This code should be stored in the first sector of the hard disk.
# After the BIOS initializes the hardware on startup or system reset,
# it loads this code at physical address 0x7c00 - 0x7d00 (512 bytes).
# Then the BIOS jumps to the beginning of it, address 0x7c00,
# while running in 16-bit real-mode (8086 compatibility mode).
# The Code Segment register (CS) is initially zero on entry.
#
#
# This code switches into 32-bit protected mode so that all of
# memory can accessed, then calls into C.
###################################################################################

.globl start # Entry point
start: .code16 # This runs in real mode
cli # Disable interrupts
cld # String operations increment

.globl start # Entry point
start:
.code16 # This runs in real mode
cli # Disable interrupts
cld # String operations increment

# Set up the important data segment registers (DS, ES, SS).
xorw %ax,%ax # Segment number zero
movw %ax,%ds # -> Data Segment
movw %ax,%es # -> Extra Segment
movw %ax,%ss # -> Stack Segment
# Set up the important data segment registers (DS, ES, SS).
xorw %ax,%ax # Segment number zero
movw %ax,%ds # -> Data Segment
movw %ax,%es # -> Extra Segment
movw %ax,%ss # -> Stack Segment

# Set up the stack pointer, growing downward from 0x7c00.
movw $start,%sp # Stack Pointer
# Set up the stack pointer, growing downward from 0x7c00.
movw $start,%sp # Stack Pointer
#### Enable A20:
#### For fascinating historical reasons (related to the fact that
#### the earliest 8086-based PCs could only address 1MB of physical memory
#### and subsequent 80286-based PCs wanted to retain maximum compatibility),
#### physical address line 20 is tied to low when the machine boots.
#### Obviously this a bit of a drag for us, especially when trying to
#### address memory above 1MB. This code undoes this.

seta20.1: inb $0x64,%al # Get status
testb $0x2,%al # Busy?
jnz seta20.1 # Yes
movb $0xd1,%al # Command: Write
outb %al,$0x64 # output port
seta20.2: inb $0x64,%al # Get status
testb $0x2,%al # Busy?
jnz seta20.2 # Yes
movb $0xdf,%al # Enable
outb %al,$0x60 # A20

seta20.1:
inb $0x64,%al # Get status
testb $0x2,%al # Busy?
jnz seta20.1 # Yes
movb $0xd1,%al # Command: Write
outb %al,$0x64 # output port

#### Switch from real to protected mode
seta20.2:
inb $0x64,%al # Get status
testb $0x2,%al # Busy?
jnz seta20.2 # Yes
movb $0xdf,%al # Enable
outb %al,$0x60 # A20

#### Switch from real to protected mode
#### The descriptors in our GDT allow all physical memory to be accessed.
#### Furthermore, the descriptors have base addresses of 0, so that the
#### segment translation is a NOP, ie. virtual addresses are identical to
#### their physical addresses. With this setup, immediately after
#### enabling protected mode it will still appear to this code
#### that it is running directly on physical memory with no translation.
#### This initial NOP-translation setup is required by the processor
#### to ensure that the transition to protected mode occurs smoothly.

real_to_prot: cli # Mandatory since we dont set up an IDT
lgdt gdtdesc # load GDT -- mandatory in protected mode
movl %cr0, %eax # turn on protected mode
orl $CR0_PE_ON, %eax #
movl %eax, %cr0 #
### CPU magic: jump to relocation, flush prefetch queue, and reload %cs
### Has the effect of just jmp to the next instruction, but simultaneous
### loads CS with $PROT_MODE_CSEG.
ljmp $PROT_MODE_CSEG, $protcseg

#### enabling protected mode it will still appear to this code
#### that it is running directly on physical memory with no translation.
#### This initial NOP-translation setup is required by the processor
#### to ensure that the transition to protected mode occurs smoothly.

real_to_prot:
cli # Mandatory since we dont set up an IDT
lgdt gdtdesc # load GDT -- mandatory in protected mode
movl %cr0, %eax # turn on protected mode
orl $CR0_PE_ON, %eax #
movl %eax, %cr0 #
### CPU magic: jump to relocation, flush prefetch queue, and reload %cs
### Has the effect of just jmp to the next instruction, but simultaneous
### loads CS with $PROT_MODE_CSEG.
ljmp $PROT_MODE_CSEG, $protcseg

#### we are in 32-bit protected mode (hence the .code32)
.code32
protcseg:
# Set up the protected-mode data segment registers
movw $PROT_MODE_DSEG, %ax # Our data segment selector
movw %ax, %ds # -> DS: Data Segment
movw %ax, %es # -> ES: Extra Segment
movw %ax, %fs # -> FS
movw %ax, %gs # -> GS
movw %ax, %ss # -> SS: Stack Segment

call cmain # finish the boot load from C.
# cmain() should not return
spin: jmp spin # ..but in case it does, spin

.p2align 2 # force 4 byte alignment
protcseg:
# Set up the protected-mode data segment registers
movw $PROT_MODE_DSEG, %ax # Our data segment selector
movw %ax, %ds # -> DS: Data Segment
movw %ax, %es # -> ES: Extra Segment
movw %ax, %fs # -> FS
movw %ax, %gs # -> GS
movw %ax, %ss # -> SS: Stack Segment

call cmain # finish the boot load from C.
# cmain() should not return
spin:
jmp spin # ..but in case it does, spin


.p2align 2 # force 4 byte alignment
gdt:
SEG_NULLASM # null seg
SEG_ASM(STA_X|STA_R, 0x0, 0xffffffff) # code seg
SEG_ASM(STA_W, 0x0, 0xffffffff) # data seg
SEG_NULLASM # null seg
SEG_ASM(STA_X|STA_R, 0x0, 0xffffffff) # code seg
SEG_ASM(STA_W, 0x0, 0xffffffff) # data seg
gdtdesc:
.word 0x17 # sizeof(gdt) - 1
.long gdt # address gdt
.word 0x17 # sizeof(gdt) - 1
.long gdt # address gdt
112 changes: 56 additions & 56 deletions bootmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
* be stored in the first sector of the disk.
*
* * The 2nd sector onward holds the kernel image.
*
*
* * The kernel image must be in ELF format.
*
* BOOT UP STEPS
* BOOT UP STEPS
* * when the CPU boots it loads the BIOS into memory and executes it
*
* * the BIOS intializes devices, sets of the interrupt routines, and
Expand All @@ -30,92 +30,92 @@
* * cmain() in this file takes over, reads in the kernel and jumps to it.
**********************************************************************/

#define SECTSIZE 512
#define ELFHDR ((struct elfhdr *) 0x10000) // scratch space
#define SECTSIZE 512
#define ELFHDR ((struct elfhdr *) 0x10000) // scratch space

void readsect(void*, uint);
void readseg(uint, uint, uint);

void
cmain(void)
{
struct proghdr *ph, *eph;
struct proghdr *ph, *eph;

// read 1st page off disk
readseg((uint) ELFHDR, SECTSIZE*8, 0);
// read 1st page off disk
readseg((uint) ELFHDR, SECTSIZE*8, 0);

// is this a valid ELF?
if (ELFHDR->magic != ELF_MAGIC)
goto bad;
// is this a valid ELF?
if (ELFHDR->magic != ELF_MAGIC)
goto bad;

// load each program segment (ignores ph flags)
ph = (struct proghdr *) ((uchar *) ELFHDR + ELFHDR->phoff);
eph = ph + ELFHDR->phnum;
for (; ph < eph; ph++)
readseg(ph->va, ph->memsz, ph->offset);
// load each program segment (ignores ph flags)
ph = (struct proghdr *) ((uchar *) ELFHDR + ELFHDR->phoff);
eph = ph + ELFHDR->phnum;
for (; ph < eph; ph++)
readseg(ph->va, ph->memsz, ph->offset);

// call the entry point from the ELF header
// note: does not return!
((void (*)(void)) (ELFHDR->entry & 0xFFFFFF))();
// call the entry point from the ELF header
// note: does not return!
((void (*)(void)) (ELFHDR->entry & 0xFFFFFF))();

bad:
outw(0x8A00, 0x8A00);
outw(0x8A00, 0x8E00);
while(1)
/* do nothing */;
outw(0x8A00, 0x8A00);
outw(0x8A00, 0x8E00);
while(1)
/* do nothing */;
}

// Read 'count' bytes at 'offset' from kernel into virtual address 'va'.
// Might copy more than asked
void
readseg(uint va, uint count, uint offset)
{
uint end_va;

va &= 0xFFFFFF;
end_va = va + count;
// round down to sector boundary
va &= ~(SECTSIZE - 1);

// translate from bytes to sectors, and kernel starts at sector 1
offset = (offset / SECTSIZE) + 1;

// If this is too slow, we could read lots of sectors at a time.
// We'd write more to memory than asked, but it doesn't matter --
// we load in increasing order.
while (va < end_va) {
readsect((uchar*) va, offset);
va += SECTSIZE;
offset++;
}
uint end_va;

va &= 0xFFFFFF;
end_va = va + count;
// round down to sector boundary
va &= ~(SECTSIZE - 1);

// translate from bytes to sectors, and kernel starts at sector 1
offset = (offset / SECTSIZE) + 1;

// If this is too slow, we could read lots of sectors at a time.
// We'd write more to memory than asked, but it doesn't matter --
// we load in increasing order.
while (va < end_va) {
readsect((uchar*) va, offset);
va += SECTSIZE;
offset++;
}
}

void
waitdisk(void)
{
// wait for disk reaady
while ((inb(0x1F7) & 0xC0) != 0x40)
/* do nothing */;
// wait for disk reaady
while ((inb(0x1F7) & 0xC0) != 0x40)
/* do nothing */;
}

void
readsect(void *dst, uint offset)
{
// wait for disk to be ready
waitdisk();
// wait for disk to be ready
waitdisk();

outb(0x1F2, 1); // count = 1
outb(0x1F3, offset);
outb(0x1F4, offset >> 8);
outb(0x1F5, offset >> 16);
outb(0x1F6, (offset >> 24) | 0xE0);
outb(0x1F7, 0x20); // cmd 0x20 - read sectors
outb(0x1F2, 1); // count = 1
outb(0x1F3, offset);
outb(0x1F4, offset >> 8);
outb(0x1F5, offset >> 16);
outb(0x1F6, (offset >> 24) | 0xE0);
outb(0x1F7, 0x20); // cmd 0x20 - read sectors

// wait for disk to be ready
waitdisk();
// wait for disk to be ready
waitdisk();

// read a sector
insl(0x1F0, dst, SECTSIZE/4);
// read a sector
insl(0x1F0, dst, SECTSIZE/4);
}

Loading

0 comments on commit a650c60

Please sign in to comment.