Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

309552035 lab7 lab8 #239

Open
wants to merge 5 commits into
base: 309552035
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/include",
"${workspaceFolder}/include"
],
"defines": [],
"compilerPath": "/usr/bin/aarch64-linux-gnu-g++",
Expand Down
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ OBJCOPY := aarch64-linux-gnu-objcopy
CCFLAGS := -std=gnu17
CXXFLAGS := -ffreestanding -nostdinc -nostdlib -nostdinc++ -nostartfiles -g -std=c++17 -MMD -I$(CURDIR)/include
export
PROGS := $(patsubst %,initramfs/%,$(filter-out include lib,$(patsubst program/%,%,$(shell find program -maxdepth 1 -mindepth 1 -type d))))
ELFS := $(patsubst initramfs/%,program/%.elf,$(PROGS))
PROGS := $(patsubst %,initramfs/%,$(filter-out include lib,$(patsubst program/%,%,$(shell find program -maxdepth 1 -mindepth 1 -type d))))
ELFS := $(patsubst initramfs/%,program/%.elf,$(PROGS))
QEMU := qemu-system-aarch64
QEMU_ARG := -M raspi3 -kernel kernel8.img -initrd initramfs.cpio -display none -serial null -serial stdio -semihosting -drive if=sd,file=nctuos.img,format=raw

.PHONY: all inter_kernel

Expand Down Expand Up @@ -42,7 +44,7 @@ clean:
rm -rf initramfs initramfs.cpio kernel8.img

run: all
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -initrd initramfs.cpio -display none -serial null -serial stdio -semihosting
$(QEMU) $(QEMU_ARG)

qemu-debug: all
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -initrd initramfs.cpio -display none -serial null -serial stdio -semihosting -S -s
$(QEMU) $(QEMU_ARG) -S -s
29 changes: 29 additions & 0 deletions include/kernel/fat32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <types.h>

struct mbr_table_entry
{
uint32_t first_sector_addr;
uint32_t total_sectors;
};

struct mbr
{
struct mbr_table_entry entry[4];
};

struct fat32 {
uint16_t bytes_pre_logical_sector;
uint8_t logical_sector_per_cluster;
uint16_t reserved_logical_sector_size;
uint8_t fat_count;
uint32_t fat_size; // 0x24
uint32_t root_dir_start; // 0x2c
uint16_t fs_info_size; // 0x30

uint32_t fat_start_block;
uint32_t data_start_block;
};

void get_fat(struct fat32 *fat, char *sector_buffer);
4 changes: 4 additions & 0 deletions include/kernel/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ struct file_entry {
char filename[24];
uint32_t next_free_entry;
};

uint32_t fat32_entry;
uint32_t fat32_block;

uint32_t block;
uint32_t file_size;
Expand All @@ -23,6 +26,7 @@ struct fd_entry {
uint32_t mode;
};

void fs_init();
int open(char *filename, int mode);
size_t read(int fd, char *buf, size_t count);
size_t write(int fd, char *buf, size_t count);
Expand Down
21 changes: 11 additions & 10 deletions include/kernel/memory_addr.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#pragma once

#define INITRAMFS_BASE 0x08000000
#define BUDDY_INFO_BASE 0x10000000
#define TASK_STRUCT_BASE 0x11000000
#define MEM_ALLOC_PTR_BASE 0x12000000
#define MOMORY_ALLOC_BASE 0x13000000
#define MMIO_BASE 0x3F000000
#define CPU_MMIO_BASE 0x40000000
#define FILE_ENTRY_BASE 0x14000000
#define FILE_BLOCK_ENTRY 0x15000000
#define FILE_CONTENT_BASE 0x16000000
#define INITRAMFS_BASE 0xFFFF000008000000
#define BUDDY_INFO_BASE 0xFFFF000010000000
#define TASK_STRUCT_BASE 0xFFFF000011000000
#define MEM_ALLOC_PTR_BASE 0xFFFF000012000000
#define MOMORY_ALLOC_BASE 0xFFFF000013000000
#define MMIO_BASE 0xFFFF00003F000000
#define CPU_MMIO_BASE 0xFFFF000040000000
#define FILE_ENTRY_BASE 0xFFFF000014000000
#define FILE_BLOCK_ENTRY 0xFFFF000015000000
#define FILE_CONTENT_BASE 0xFFFF000016000000
#define PAGE_TABLE_ADDR 0xFFFF000000004400
1 change: 1 addition & 0 deletions include/kernel/memory_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
extern "C" {
void *memset(void *s, int c, size_t n);
void *memcpy(void *dest, const void *src, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
}
2 changes: 1 addition & 1 deletion include/kernel/mmio.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum class MMIOREG : uint64_t {

class MMIO {
public:
static const uint32_t PM_PASSWORD = 0x5A000000;
static constexpr uint32_t PM_PASSWORD = 0x5A000000;
static inline void set(MMIOREG addr, uint32_t val) {
*reinterpret_cast<volatile uint32_t*>(addr) = val;
}
Expand Down
5 changes: 5 additions & 0 deletions include/kernel/sdhost.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

void readblock(int block_idx, void* buf);
void writeblock(int block_idx, void* buf);
void sd_init();
14 changes: 13 additions & 1 deletion include/kernel/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ struct task_reg_struct {
void *sp;
};

struct page_table_struct {
uint64_t stack_alloc;
uint64_t program_alloc;
};

struct alignas(16) task_struct {
task_reg_struct regs;
void *stack_alloc;
void *kernel_stack_alloc;
void *program_alloc;
void *fd_entries;
page_table_struct *page_table;
uint64_t pid;
uint64_t sleep_until;
uint64_t program_size;
Expand All @@ -35,13 +40,20 @@ struct alignas(16) task_struct {
int64_t first_untouched_fd;
};

#define NON_EXECUTE_FLAG 0x40000000000000
#define USER_FLAG 0x447

extern task_struct* tasks;

inline void* kernel_to_physical(void *addr) {
return (void*)(uint64_t(addr) & 0xffffffffffff);
}

void task_init();

extern "C" {
void do_exit();
void switch_to(task_struct *from, task_struct *to, uint64_t to_tpidr, ...);
void switch_to(task_reg_struct *from, task_reg_struct *to, page_table_struct *page, uint64_t to_tpidr, ...);
uint64_t get_tpidr_el1();
void loop();
void set_eret_addr(void *addr);
Expand Down
52 changes: 52 additions & 0 deletions kernel/fat32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <kernel/fat32.h>
#include <kernel/sdhost.h>
#include <kernel/memory_func.h>
#include <kernel/mini_uart.h>
#include <kernel/string.h>

void get_mbr(struct mbr *mbr, char *buffer) {
readblock(0, buffer);
char buffer2[20];
for (int i = 0 ;i < 4; i++) {
memcpy(&mbr->entry[i].first_sector_addr, &buffer[446 + i * 16 + 8], 4);
memcpy(&mbr->entry[i].total_sectors, &buffer[446 + i * 16 + 12], 4);
io() << i << " first_sector_addr: " << u64tohex(mbr->entry[i].first_sector_addr, buffer2, sizeof(buffer2)) << "\r\n";
io() << i << " total_sectors: " << u64tohex(mbr->entry[i].total_sectors, buffer2, sizeof(buffer2)) << "\r\n";
}
}

void get_fat(struct fat32 *fat, char *sector_buffer) {
struct mbr mbr;
char buffer2[20];
io() << "Checkpoint 1\r\n";
get_mbr(&mbr, sector_buffer);
io() << "Checkpoint 2\r\n";
int fat32_start_sector = mbr.entry[0].first_sector_addr;
io() << "Checkpoint 3\r\n";
readblock(fat32_start_sector, sector_buffer);
io() << "Checkpoint 4\r\n";
memcpy(&fat->bytes_pre_logical_sector, &sector_buffer[0xb], 2);
memcpy(&fat->logical_sector_per_cluster, &sector_buffer[0xd], 1);
memcpy(&fat->reserved_logical_sector_size, &sector_buffer[0xe], 2);
memcpy(&fat->fat_count, &sector_buffer[0x10], 1);
memcpy(&fat->fat_size, &sector_buffer[0x24], 4);
memcpy(&fat->root_dir_start, &sector_buffer[0x2c], 4);
memcpy(&fat->fs_info_size, &sector_buffer[0x30], 2);

fat->fat_start_block = fat32_start_sector + fat->reserved_logical_sector_size;
fat->data_start_block = fat->fat_start_block + fat->fat_count * fat->fat_size;
io() << "bytes_pre_logical_sector: " << u64tohex(fat->bytes_pre_logical_sector, buffer2, sizeof(buffer2)) << "\r\n";
io() << "logical_sector_per_cluster: " << u64tohex(fat->logical_sector_per_cluster, buffer2, sizeof(buffer2)) << "\r\n";
io() << "reserved_logical_sector_size: " << u64tohex(fat->reserved_logical_sector_size, buffer2, sizeof(buffer2)) << "\r\n";
io() << "fat_count: " << u64tohex(fat->fat_count, buffer2, sizeof(buffer2)) << "\r\n";
io() << "fat_size: " << u64tohex(fat->fat_size, buffer2, sizeof(buffer2)) << "\r\n";
io() << "root_dir_start: " << u64tohex(fat->root_dir_start, buffer2, sizeof(buffer2)) << "\r\n";
io() << "fs_info_size: " << u64tohex(fat->fs_info_size, buffer2, sizeof(buffer2)) << "\r\n";
io() << "fat_start_block: " << u64tohex(fat->fat_start_block, buffer2, sizeof(buffer2)) << "\r\n";
io() << "data_start_block: " << u64tohex(fat->data_start_block, buffer2, sizeof(buffer2)) << "\r\n";
io() << "Checkpoint 5\r\n";
readblock(fat->fat_start_block, sector_buffer);
io() << "Checkpoint 6\r\n";
readblock(fat->data_start_block, sector_buffer);
io() << "Checkpoint 7\r\n";
}
Loading