Skip to content

Commit

Permalink
ipu init
Browse files Browse the repository at this point in the history
  • Loading branch information
hamadakafu committed Apr 10, 2022
1 parent eeb7b41 commit 523a7b2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
Binary file added a.out
Binary file not shown.
59 changes: 33 additions & 26 deletions bootmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,75 @@
#include "x86.h"
#include "memlayout.h"

#define SECTSIZE 512
#define SECTSIZE 512

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

void
bootmain(void)
void bootmain(void)
{
struct elfhdr *elf;
struct proghdr *ph, *eph;
void (*entry)(void);
uchar* pa;
uchar *pa;

elf = (struct elfhdr*)0x10000; // scratch space
elf = (struct elfhdr *)0x10000; // scratch space

// Read 1st page off disk
readseg((uchar*)elf, 4096, 0);
readseg((uchar *)elf, 4096, 0);

// Is this an ELF executable?
if(elf->magic != ELF_MAGIC)
return; // let bootasm.S handle error
if (elf->magic != ELF_MAGIC)
return; // let bootasm.S handle error

// Load each program segment (ignores ph flags).
ph = (struct proghdr*)((uchar*)elf + elf->phoff);
ph = (struct proghdr *)((uchar *)elf + elf->phoff);
eph = ph + elf->phnum;
for(; ph < eph; ph++){
pa = (uchar*)ph->paddr;
for (; ph < eph; ph++)
{
pa = (uchar *)ph->paddr;
readseg(pa, ph->filesz, ph->off);
if(ph->memsz > ph->filesz)
if (ph->memsz > ph->filesz)
stosb(pa + ph->filesz, 0, ph->memsz - ph->filesz);
}

// Call the entry point from the ELF header.
// Does not return!
entry = (void(*)(void))(elf->entry);
entry = (void (*)(void))(elf->entry);
entry();
}

void
waitdisk(void)
void waitdisk(void)
{
// Wait for disk ready.
while((inb(0x1F7) & 0xC0) != 0x40)
while ((inb(0x1F7) & 0xC0) != 0x40)
;
}

// Read a single sector at offset into dst.
void
readsect(void *dst, uint offset)
void readsect(void *dst, uint offset)
{
// Issue command.
waitdisk();
outb(0x1F2, 1); // count = 1
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(0x1F7, 0x20); // cmd 0x20 - read sectors

// Read data.
waitdisk();
insl(0x1F0, dst, SECTSIZE/4);
insl(0x1F0, dst, SECTSIZE / 4);
}

// Read 'count' bytes at 'offset' from kernel into physical address 'pa'.
// Might copy more than asked.
void
readseg(uchar* pa, uint count, uint offset)
// pa: physical address
// count: number of bytes to read
// offset: disk offset by bytes
void readseg(uchar *pa, uint count, uint offset)
{
uchar* epa;
uchar *epa;

epa = pa + count;

Expand All @@ -91,6 +91,13 @@ readseg(uchar* pa, uint count, uint offset)
// 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.
for(; pa < epa; pa += SECTSIZE, offset++)
for (; pa < epa; pa += SECTSIZE, offset++)
readsect(pa, offset);
}

// readseg(100, 512, 10);
// pa == 90
// offset: block単位に変わる
// 0sector目はbootloader
// 1sector目からkernel
// offsetはdiskの何セクター目を読むか
2 changes: 1 addition & 1 deletion entry.S
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# The xv6 kernel starts executing in this file. This file is linked with
# the kernel C code, so it can refer to kernel symbols such as main().
# The boot block (bootasm.S and bootmain.c) jumps to entry below.

# Multiboot header, for multiboot boot loaders like GNU Grub.
# http://www.gnu.org/software/grub/manual/multiboot/multiboot.html
#
Expand Down
5 changes: 5 additions & 0 deletions sample.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>
int main() {
static long int aaaa[0x1000000];
return 0;
}
2 changes: 2 additions & 0 deletions sample1.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dummy_label:
nop

0 comments on commit 523a7b2

Please sign in to comment.