Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Lab3/kernel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
ARMGNU ?= aarch64-linux-gnu

COPS = -Iinclude -Wall -ffreestanding -fno-stack-protector -nostdlib -nostartfiles -mgeneral-regs-only
ASMOPS = -Iinclude -ggdb

BUILD_DIR = build
SRC_DIR = src

C_FILES = $(wildcard $(SRC_DIR)/*.c)
ASM_FILES = $(wildcard $(SRC_DIR)/*.S)
OBJ_FILES = $(C_FILES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%_c.o)
OBJ_FILES += $(ASM_FILES:$(SRC_DIR)/%.S=$(BUILD_DIR)/%_s.o)

DEP_FILES = $(OBJ_FILES:%.o=%.d)
-include $(DEP_FILES)

all : kernel8.img

clean :
rm -rf $(BUILD_DIR)/* *.img

run :
qemu-system-aarch64 -M raspi3b -serial null -serial stdio -display none -kernel kernel8.img -initrd initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb

test :
qemu-system-aarch64 -M raspi3b -serial null -serial stdio -display none -initrd initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb -kernel kernel8.img -S -s

gdb :
aarch64-linux-gnu-gdb build/kernel8.elf

$(BUILD_DIR)/%_c.o: $(SRC_DIR)/%.c
$(ARMGNU)-gcc $(COPS) -MMD -c $< -o $@

$(BUILD_DIR)/%_s.o: $(SRC_DIR)/%.S
$(ARMGNU)-gcc $(ASMOPS) -MMD -c $< -o $@

kernel8.img: $(SRC_DIR)/linker.ld $(OBJ_FILES)
$(ARMGNU)-ld -T $(SRC_DIR)/linker.ld -Map $(BUILD_DIR)/kernel8.map -o $(BUILD_DIR)/kernel8.elf $(OBJ_FILES)
$(ARMGNU)-objcopy $(BUILD_DIR)/kernel8.elf -O binary kernel8.img

#qemu-system-aarch64 -M raspi3b -kernel kernel8.img -nographic -serial null -chardev stdio,id=uart1 -serial chardev:uart1 -monitor none

31 changes: 31 additions & 0 deletions Lab3/kernel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# OSC2024

| Github Account | Student ID | Name |
|----------------|------------|---------------|
| nien-zhu | 312515040 | Nien-Chu Yu |

## Requirements

* cross-compiler: aarch64-linux-gnu-gcc
* Emulator: qemu-system-aarch64

## Build

```
make
```

## Debug

```
make test
make gdb
```

## Test With QEMU

```
make run
(qemu-system-aarch64 -M raspi3b -serial null -serial stdio -display none -kernel kernel8.img)
```

Binary file added Lab3/kernel/bcm2710-rpi-3-b-plus.dtb
Binary file not shown.
1 change: 1 addition & 0 deletions Lab3/kernel/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
initramfs initramfs.cpio 0x20000000
1 change: 1 addition & 0 deletions Lab3/kernel/include/allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void* simple_malloc(unsigned long size);
39 changes: 39 additions & 0 deletions Lab3/kernel/include/cpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

extern char * cpio_addr;

void cpio_ls();
void cpio_cat(char* filename);
int cpio_TRAILER_compare(const char* str1);
void cpio_load_program(char *filename);

struct new_cpio_header {
// uses 8-byte hexadecimal fields for all numbers
char c_magic[6]; //determine whether this archive is written with little-endian or big-endian integers.
char c_ino[8]; //determine when two entries refer to the same file.
char c_mode[8]; //specifies both the regular permissions and the file type.
char c_uid[8]; // numeric user id
char c_gid[8]; // numeric group id
char c_nlink[8]; // number of links to this file.
char c_mtime[8]; // Modification time of the file
char c_filesize[8]; // size of the file
char c_devmajor[8];
char c_devminor[8];
char c_rdevmajor[8];
char c_rdevminor[8];
char c_namesize[8]; // number of bytes in the pathname
char c_check[8]; // always set to zero by writers and ignored by readers.
};

struct old_cpio_header {
char c_magic[6];
char c_dev[6];
char c_ino[6];
char c_mode[6];
char c_uid[6];
char c_gid[6];
char c_nlink[6];
char c_rdev[6];
char c_mtime[11];
char c_namesize[6];
char c_filesize[11];
};
43 changes: 43 additions & 0 deletions Lab3/kernel/include/dtb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stddef.h>
#include <stdint.h>


#define FDT_BEGIN_NODE (0x00000001)
#define FDT_END_NODE (0x00000002)
#define FDT_PROP (0x00000003)
#define FDT_NOP (0x00000004)
#define FDT_END (0x00000009)

typedef void (*fdt_callback)(int node,const char* name,const void* data,uint32_t size);

uint32_t fdt_u32_le2be (const void *addr);
int fdt_traverse(fdt_callback cb, void* _dtb);
int struct_parse(fdt_callback cb, uintptr_t cur_ptr, uintptr_t strings_ptr,uint32_t totalsize);
void get_cpio_addr(int token,const char* name,const void* data,uint32_t size);
void print_dtb(int token, const char* name, const void* data, uint32_t size);
void data_parse(const char* name, const void* data, uint32_t len);



struct fdt_header {
uint32_t magic;
uint32_t totalsize;
uint32_t off_dt_struct;
uint32_t off_dt_strings;
uint32_t off_mem_rsvmap;
uint32_t version;
uint32_t last_comp_version;
uint32_t boot_cpuid_phys;
uint32_t size_dt_strings;
uint32_t size_dt_struct;
};

struct fdt_reserve_entry {
uint64_t address;
uint64_t size;
};

struct fdt_property{
uint32_t len;
uint32_t nameoff;
};
4 changes: 4 additions & 0 deletions Lab3/kernel/include/except_c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void exception_entry();
void core_timer_exception_entry();
void irq_exception_entry();
void test_entry();
38 changes: 38 additions & 0 deletions Lab3/kernel/include/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef GPIO_H
#define GPIO_H

#include <stdint.h>

#define MMIO_BASE 0x3F000000

#define GPFSEL0 ((volatile unsigned int*)(MMIO_BASE+0x00200000))
#define GPFSEL1 ((volatile unsigned int*)(MMIO_BASE+0x00200004))
#define GPFSEL2 ((volatile unsigned int*)(MMIO_BASE+0x00200008))
#define GPFSEL3 ((volatile unsigned int*)(MMIO_BASE+0x0020000C))
#define GPFSEL4 ((volatile unsigned int*)(MMIO_BASE+0x00200010))
#define GPFSEL5 ((volatile unsigned int*)(MMIO_BASE+0x00200014))
#define GPSET0 ((volatile unsigned int*)(MMIO_BASE+0x0020001C))
#define GPSET1 ((volatile unsigned int*)(MMIO_BASE+0x00200020))
#define GPCLR0 ((volatile unsigned int*)(MMIO_BASE+0x00200028))
#define GPLEV0 ((volatile unsigned int*)(MMIO_BASE+0x00200034))
#define GPLEV1 ((volatile unsigned int*)(MMIO_BASE+0x00200038))
#define GPEDS0 ((volatile unsigned int*)(MMIO_BASE+0x00200040))
#define GPEDS1 ((volatile unsigned int*)(MMIO_BASE+0x00200044))
#define GPHEN0 ((volatile unsigned int*)(MMIO_BASE+0x00200064))
#define GPHEN1 ((volatile unsigned int*)(MMIO_BASE+0x00200068))
#define GPPUD ((volatile unsigned int*)(MMIO_BASE+0x00200094))
#define GPPUDCLK0 ((volatile unsigned int*)(MMIO_BASE+0x00200098))
#define GPPUDCLK1 ((volatile unsigned int*)(MMIO_BASE+0x0020009C))

// Helper function to write data to a memory-mapped I/O address
static inline void mmio_write(volatile uint32_t* reg, uint32_t data) {
*reg = data;
}

// Helper function to read data from a memory-mapped I/O address
static inline uint32_t mmio_read(volatile uint32_t* reg) {
return *reg;
}


#endif
21 changes: 21 additions & 0 deletions Lab3/kernel/include/interrupt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "gpio.h"


#define IRQ_BASIC_PENDING ((volatile unsigned int*)(MMIO_BASE+0x0000B200))
#define IRQ_PENDING_1 ((volatile unsigned int*)(MMIO_BASE+0x0000B204))
#define IRQ_PENDING_2 ((volatile unsigned int*)(MMIO_BASE+0x0000B208))
#define FIQ_CONTROL ((volatile unsigned int*)(MMIO_BASE+0x0000B20C))
#define ENABLE_IRQS_1 ((volatile unsigned int*)(MMIO_BASE+0x0000B210))
#define ENABLE_IRQS_2 ((volatile unsigned int*)(MMIO_BASE+0x0000B214))
#define ENABLE_BASIC_IRQS ((volatile unsigned int*)(MMIO_BASE+0x0000B218))
#define DISABLE_IRQS_1 ((volatile unsigned int*)(MMIO_BASE+0x0000B21C))
#define DISABLE_IRQS_2 ((volatile unsigned int*)(MMIO_BASE+0x0000B220))
#define DISABLE_BASIC_IRQS ((volatile unsigned int*)(MMIO_BASE+0x0000B224))

#define SYSTEM_TIMER_IRQ_0 (1 << 0)
#define SYSTEM_TIMER_IRQ_1 (1 << 1)
#define SYSTEM_TIMER_IRQ_2 (1 << 2)
#define SYSTEM_TIMER_IRQ_3 (1 << 3)

void enable_interrupt();
void disable_interrupt();
42 changes: 42 additions & 0 deletions Lab3/kernel/include/mailbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "gpio.h"

extern volatile unsigned int mailbox[36];



/* channels */
#define MBOX_CH_POWER 0
#define MBOX_CH_FB 1
#define MBOX_CH_VUART 2
#define MBOX_CH_VCHIQ 3
#define MBOX_CH_LEDS 4
#define MBOX_CH_BTNS 5
#define MBOX_CH_TOUCH 6
#define MBOX_CH_COUNT 7
#define MBOX_CH_PROP 8

/* tags */
#define MBOX_TAG_GETBOARD 0x00010002
#define MBOX_REQUEST 0x00000000
#define MBOX_TAG_GETSERIAL 0x00010004
#define TAG_REQUEST_CODE 0x00000000
#define MBOX_TAG_GETARMMEM 0x00010005
#define MBOX_TAG_LAST 0x00000000
#define END_TAG 0x00000000

int mailbox_call();
void get_board_revision();
void get_arm_mem();

#define VIDEOCORE_MBOX (MMIO_BASE+0xB880)
#define MBOX_READ ((volatile unsigned int*)(VIDEOCORE_MBOX+0x0))
#define MBOX_POLL ((volatile unsigned int*)(VIDEOCORE_MBOX+0x10))
#define MBOX_SENDER ((volatile unsigned int*)(VIDEOCORE_MBOX+0x14))
#define MBOX_STATUS ((volatile unsigned int*)(VIDEOCORE_MBOX+0x18))
#define MBOX_CONFIG ((volatile unsigned int*)(VIDEOCORE_MBOX+0x1C))
#define MBOX_WRITE ((volatile unsigned int*)(VIDEOCORE_MBOX+0x20))
#define MBOX_RESPONSE 0x80000000
#define MBOX_FULL 0x80000000
#define MBOX_EMPTY 0x40000000
#define REQUEST_SUCCEED 0x80000000
#define REQUEST_FAILED 0x80000001
106 changes: 106 additions & 0 deletions Lab3/kernel/include/printf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
File: printf.h

Copyright (C) 2004 Kustaa Nyholm

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

This library is realy just two files: 'printf.h' and 'printf.c'.

They provide a simple and small (+200 loc) printf functionality to
be used in embedded systems.

I've found them so usefull in debugging that I do not bother with a
debugger at all.

They are distributed in source form, so to use them, just compile them
into your project.

Two printf variants are provided: printf and sprintf.

The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.

Zero padding and field width are also supported.

If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
long specifier is also
supported. Note that this will pull in some long math routines (pun intended!)
and thus make your executable noticably longer.

The memory foot print of course depends on the target cpu, compiler and
compiler options, but a rough guestimate (based on a H8S target) is about
1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
Not too bad. Your milage may vary. By hacking the source code you can
get rid of some hunred bytes, I'm sure, but personally I feel the balance of
functionality and flexibility versus code size is close to optimal for
many embedded systems.

To use the printf you need to supply your own character output function,
something like :

void putc ( void* p, char c)
{
while (!SERIAL_PORT_EMPTY) ;
SERIAL_PORT_TX_REGISTER = c;
}

Before you can call printf you need to initialize it to use your
character output function with something like:

init_printf(NULL,putc);

Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
the NULL (or any pointer) you pass into the 'init_printf' will eventually be
passed to your 'putc' routine. This allows you to pass some storage space (or
anything realy) to the character output function, if necessary.
This is not often needed but it was implemented like that because it made
implementing the sprintf function so neat (look at the source code).

The code is re-entrant, except for the 'init_printf' function, so it
is safe to call it from interupts too, although this may result in mixed output.
If you rely on re-entrancy, take care that your 'putc' function is re-entrant!

The printf and sprintf functions are actually macros that translate to
'tfp_printf' and 'tfp_sprintf'. This makes it possible
to use them along with 'stdio.h' printf's in a single source file.
You just need to undef the names before you include the 'stdio.h'.
Note that these are not function like macros, so if you have variables
or struct members with these names, things will explode in your face.
Without variadic macros this is the best we can do to wrap these
fucnction. If it is a problem just give up the macros and use the
functions directly or rename them.

For further details see source code.

regs Kusti, 23.10.2004
*/


#ifndef __TFP_PRINTF__
#define __TFP_PRINTF__

#include <stdarg.h>

void init_printf(void* putp,void (*putf) (void*,char));

void tfp_printf(char *fmt, ...);
void tfp_sprintf(char* s,char *fmt, ...);

void tfp_format(void* putp,void (*putf) (void*,char),char *fmt, va_list va);

#define printf tfp_printf
#define sprintf tfp_sprintf

#endif
7 changes: 7 additions & 0 deletions Lab3/kernel/include/reboot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define PM_PASSWORD 0x5a000000
#define PM_RSTC 0x3F10001c
#define PM_WDOG 0x3F100024

void set(long addr, unsigned int value);
void reset(int tick);
void cancel_reset();
1 change: 1 addition & 0 deletions Lab3/kernel/include/shell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void shell();
Loading