Skip to content

Commit

Permalink
finished file-related syscalls chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
josehu07 committed Aug 19, 2021
1 parent 8fdec57 commit 1e8289e
Show file tree
Hide file tree
Showing 21 changed files with 804 additions and 308 deletions.
38 changes: 21 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ S_SOURCES=$(shell find ./src/ -name "*.s")
S_OBJECTS=$(patsubst %.s, %.o, $(S_SOURCES))

INIT_SOURCE=./user/init.c
INIT_OBJECT=./user/init.o
INIT_LINKED=./user/init.out
INIT_OBJECT=./user/init.c.o
INIT_LINKED=./user/init.bin
INIT_BINARY=./user/init

ULIB_C_SOURCES=$(shell find ./user/lib/ -name "*.c")
Expand All @@ -28,8 +28,10 @@ ULIB_C_OBJECTS=$(patsubst %.c, %.o, $(ULIB_C_SOURCES))
ULIB_S_SOURCES=$(shell find ./user/lib/ -name "*.s")
ULIB_S_OBJECTS=$(patsubst %.s, %.o, $(ULIB_S_SOURCES))

USER_SOURCES=$(filter-out $(INIT_SOURCE), $(shell find ./user/ -maxdepth 1 -name "*.c"))
USER_BINARYS=$(patsubst %.c, %.bin, $(USER_SOURCES))
USER_SOURCES_ALL=$(shell find ./user/ -name "*.c" ! -path "./user/lib/*")
USER_SOURCES=$(filter-out $(INIT_SOURCE), $(USER_SOURCES_ALL))
USER_OBJECTS=$(patsubst %.c, %.c.o, $(USER_SOURCES))
USER_LINKEDS=$(patsubst %.c, %.bin, $(USER_SOURCES))

FILESYS_IMG=vsfs.img

Expand Down Expand Up @@ -59,7 +61,7 @@ HUX_MSG="[--Hux->]"


ALL_DEPS := $(S_OBJECTS) $(C_OBJECTS)
ALL_DEPS += $(ULIB_S_OBJECTS) $(ULIB_C_OBJECTS) $(USER_BINARYS) initproc
ALL_DEPS += $(ULIB_S_OBJECTS) $(ULIB_C_OBJECTS) $(USER_LINKEDS) initproc
ALL_DEPS += filesys kernel verify update
all: $(ALL_DEPS)

Expand Down Expand Up @@ -88,7 +90,7 @@ $(ULIB_C_OBJECTS): %.o: %.c
@echo $(HUX_MSG) "Compiling user lib C code '$<'..."
$(CC) $(C_FLAGS_USER) -o $@ $<

$(USER_BINARYS): %.bin: %.c $(ULIB_S_OBJECTS) $(ULIB_C_OBJECTS)
$(USER_LINKEDS): %.bin: %.c $(ULIB_S_OBJECTS) $(ULIB_C_OBJECTS)
@echo
@echo $(HUX_MSG) "Compiling & linking user program '$<'..."
$(CC) $(C_FLAGS_USER) -o $<.o $<
Expand All @@ -110,8 +112,9 @@ initproc: $(INIT_SOURCE) $(ULIB_S_OBJECTS) $(ULIB_C_OBJECTS)
kernel: $(S_OBJECTS) $(C_OBJECTS) initproc
@echo
@echo $(HUX_MSG) "Linking kernel image..."
$(LD) $(LD_FLAGS) -T scripts/kernel.ld -lgcc -o $(TARGET_BIN) -Wl,--oformat,elf32-i386 \
$(S_OBJECTS) $(C_OBJECTS) -Wl,-b,binary,$(INIT_BINARY)
$(LD) $(LD_FLAGS) -T scripts/kernel.ld -lgcc -o $(TARGET_BIN) \
-Wl,--oformat,elf32-i386 $(S_OBJECTS) $(C_OBJECTS) \
-Wl,-b,binary,$(INIT_BINARY)
$(OBJCOPY) --only-keep-debug $(TARGET_BIN) $(TARGET_SYM)
$(OBJCOPY) --strip-debug $(TARGET_BIN)

Expand All @@ -123,20 +126,20 @@ kernel: $(S_OBJECTS) $(C_OBJECTS) initproc
filesys:
@echo
@echo $(HUX_MSG) "Making the file system image..."
python3 scripts/mkfs.py $(FILESYS_IMG) $(USER_BINARYS)
python3 scripts/mkfs.py $(FILESYS_IMG) $(USER_LINKEDS)


#
# Verify GRUB multiboot sanity.
#
.PHONY: verify
verify:
@if grub-file --is-x86-multiboot $(TARGET_BIN); then \
echo; \
echo $(HUX_MSG) "VERIFY MULTIBOOT: Confirmed ✓"; \
else \
echo; \
echo $(HUX_MSG) "VERIFY MULTIBOOT: FAILED ✗"; \
@if grub-file --is-x86-multiboot $(TARGET_BIN); then \
echo; \
echo $(HUX_MSG) "VERIFY MULTIBOOT: Confirmed ✓"; \
else \
echo; \
echo $(HUX_MSG) "VERIFY MULTIBOOT: FAILED ✗"; \
fi


Expand Down Expand Up @@ -183,5 +186,6 @@ clean:
@echo
@echo $(HUX_MSG) "Cleaning the build..."
rm -f $(S_OBJECTS) $(C_OBJECTS) $(ULIB_S_OBJECTS) $(ULIB_C_OBJECTS) \
$(INIT_OBJECT) $(INIT_LINKED) $(INIT_BINARY) $(USER_BINARYS) \
$(TARGET_BIN) $(TARGET_ISO) $(TARGET_SYM) $(FILESYS_IMG)
$(INIT_OBJECT) $(INIT_LINKED) $(INIT_BINARY) \
$(USER_OBJECTS) $(USER_LINKEDS) $(FILESYS_IMG) \
$(TARGET_BIN) $(TARGET_ISO) $(TARGET_SYM)
60 changes: 54 additions & 6 deletions src/boot/elf.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* ELF format related structures.
* See http://www.cs.cmu.edu/afs/cs/academic/class/15213-f00/docs/elf.pdf.
* ELF 32-bit format related structures.
* See http://www.cs.cmu.edu/afs/cs/academic/class/15213-f00/docs/elf.pdf
*/


Expand All @@ -11,12 +11,53 @@
#include <stdint.h>


/** ELF file header. */
struct elf_file_header {
uint32_t magic; /** In little-endian on x86. */
uint8_t ident[12]; /** Rest of `e_ident`. */
uint16_t type;
uint16_t machine;
uint32_t version;
uint32_t entry;
uint32_t phoff;
uint32_t shoff;
uint32_t flags;
uint16_t ehsize;
uint16_t phentsize;
uint16_t phnum;
uint16_t shentsize;
uint16_t shnum;
uint16_t shstrndx;
} __attribute__((packed));
typedef struct elf_file_header elf_file_header_t;

/**
* For getting the type of a symbol table entry. Function type code == 0x2.
* See https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-79797.html#chapter6-tbl-21.
* ELF magic number 0x7F'E''L''F' in little endian.
* See https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html#elfid
*/
#define ELF_SYM_TYPE(info) ((info) & 0xf)
#define ELF_SYM_TYPE_FUNC 0x2
#define ELF_MAGIC 0x464C457F


/** ELF program header. */
struct elf_program_header {
uint32_t type;
uint32_t offset;
uint32_t vaddr;
uint32_t paddr;
uint32_t filesz;
uint32_t memsz;
uint32_t flags;
uint32_t align;
} __attribute__((packed));
typedef struct elf_program_header elf_program_header_t;

/** ELF program header flags. */
#define ELF_PROG_FLAG_EXEC 0x1
#define ELF_PROG_FLAG_WRITE 0x2
#define ELF_PROG_FLAG_READ 0x4

/** ELF program header types. */
#define ELF_PROG_TYPE_LOAD 0x1


/** ELF section header. */
Expand Down Expand Up @@ -46,5 +87,12 @@ struct elf_symbol {
} __attribute__((packed));
typedef struct elf_symbol elf_symbol_t;

/**
* For getting the type of a symbol table entry. Function type code == 0x2.
* See https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-79797.html#chapter6-tbl-21
*/
#define ELF_SYM_TYPE(info) ((info) & 0xf)
#define ELF_SYM_TYPE_FUNC 0x2


#endif
10 changes: 5 additions & 5 deletions src/boot/multiboot.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Multiboot1 related structures.
* See https://www.gnu.org/software/grub/manual/multiboot/multiboot.html.
* See https://www.gnu.org/software/grub/manual/multiboot/html_node/Example-OS-code.html.
* See https://www.gnu.org/software/grub/manual/multiboot/multiboot.html
* See https://www.gnu.org/software/grub/manual/multiboot/html_node/Example-OS-code.html
*/


Expand All @@ -18,7 +18,7 @@

/**
* Multiboot1 header.
* See https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Header-layout.
* See https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Header-layout
*/
struct multiboot_header {
uint32_t magic; /* Must be header magic 0x1BADB002. */
Expand All @@ -38,7 +38,7 @@ typedef struct multiboot_header multiboot_header_t;
* header table from an ELF kernel is, the size of each entry, number of
* entries, and the string table used as the index of names", stated on GRUB
* multiboot1 specification.
* See https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Boot-information-format.
* See https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Boot-information-format
*/
struct multiboot_elf_section_header_table {
uint32_t num;
Expand All @@ -51,7 +51,7 @@ typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_t

/**
* Multiboot1 information.
* See https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Boot-information-format.
* See https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Boot-information-format
*/
struct multiboot_info {
uint32_t flags; /* Multiboot info version number. */
Expand Down
2 changes: 2 additions & 0 deletions src/common/bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ inline void
bitmap_clear(bitmap_t *bitmap, uint32_t slot_no)
{
assert(slot_no < bitmap->slots);
if (slot_no == 0)
info("clearing 0");

spinlock_acquire(&(bitmap->lock));

Expand Down
Loading

0 comments on commit 1e8289e

Please sign in to comment.