Skip to content

Commit

Permalink
MacOS Support (#41)
Browse files Browse the repository at this point in the history
* thanks Apple

why do they have to make everything so difficult.

* fix find

* format

* Makefile and Instructions

adds MacOS specific instructions along with modifying the makefiles to support different util names

Co-Authored-By: Emil Kovacev <[email protected]>

* grammar

* apple silicon

---------

Co-authored-by: Emil Kovacev <[email protected]>
  • Loading branch information
Sploder12 and emilkovacev authored Sep 8, 2023
1 parent fb466c6 commit f76094b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
33 changes: 24 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
CC=gcc
CFLAGS=-Wall -Werror -g3 -Os -Wl,--oformat=binary -no-pie -m32 -s -falign-functions=4 -ffreestanding -masm=intel -fno-asynchronous-unwind-tables -I./src/lib/
LFLAGS=-melf_i386 --build-id=none
PLATFORM := $(shell uname)

ASM_BOOT_SECT_SOURCE=./src/boot/boot_sect.asm
ASM_OS_ENTRY_SOURCE=./src/boot/os_entry.asm
ifeq ($(PLATFORM), Darwin)
CC := i386-elf-gcc
LD := i386-elf-ld
OBJCOPY := gobjcopy
else
CC := gcc
LD := ld
OBJCOPY := objcopy
endif

export CC
export LD
export OBJCOPY

export CFLAGS := -Wall -Werror -g3 -Os -Wl,--oformat=binary -no-pie -m32 -s -falign-functions=4 -ffreestanding -fno-asynchronous-unwind-tables
export LFLAGS := -melf_i386 --build-id=none

ASM_BOOT_SECT_SOURCE := ./src/boot/boot_sect.asm
ASM_OS_ENTRY_SOURCE := ./src/boot/os_entry.asm

BOOT_OBJ := boot.o
OS_BIN := mOS.bin

C_FILES = $(shell find -name '*.[ch]')
C_FILES = $(shell find ./ -name '*.[ch]')

OBJ_NAMES := src/os/main.o src/os/test.o os_entry.o src/lib/video/VGA_text.o \
src/os/hard/idt.o src/os/hard/except.o src/os/hard/pic.o \
Expand All @@ -20,8 +35,8 @@ OBJ_NAMES := src/os/main.o src/os/test.o os_entry.o src/lib/video/VGA_text.o \
.PHONY: clean qemu test

$(OS_BIN): $(OBJ_NAMES) $(BOOT_OBJ)
ld $(LFLAGS) -T link.ld $(OBJ_NAMES) -o mOS.elf
objcopy -O binary mOS.elf intermediate.bin
$(LD) $(LFLAGS) -T link.ld $(OBJ_NAMES) -o mOS.elf
$(OBJCOPY) -O binary mOS.elf intermediate.bin
cat $(BOOT_OBJ) intermediate.bin > $(OS_BIN)

$(BOOT_OBJ): $(ASM_BOOT_SECT_SOURCE)
Expand All @@ -31,7 +46,7 @@ os_entry.o: $(ASM_OS_ENTRY_SOURCE)
nasm $^ -f elf32 -o $@

%.o: %.c
gcc -c $^ -o $@ $(CFLAGS)
$(CC) -c $^ -o $@ $(CFLAGS) -I./src/lib/

%.o: %.asm
nasm $^ -f elf32 -o $@
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ For contributing _fork_ this repository and then make your changes in the form o
* clang-format
* WSL (Windows only)

#### For MacOS

Use homebrew to install the dependencies using these commands:

```shell
brew link --overwrite i386-elf-binutils
brew install i386-elf-binutils i386-elf-gcc nasm binutils qemu
```

If you are on Apple Silicon, you will need to use Rosetta along with installing x86_64 libraries. This can be done by prefixing the previous commands with `arch -x86_64`.

You will need to ensure that these programs are on your PATH or `make` will not find them.

### Building

* just run `make` in the root directory of the project
Expand Down
2 changes: 1 addition & 1 deletion src/os/hard/idt.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void makeInterruptTable() {
initPIC(ISR_COUNT);

// load the IDT and then enable interrupts
__asm__ volatile("lidt [%0]" : : "r"(&idtr));
__asm__ volatile("lidt (%0)" : : "r"(&idtr));
enableInterrupts();
}

Expand Down
14 changes: 10 additions & 4 deletions src/os/hard/port_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@

#include <stdint.h>

/* NOTE:
* All inline assembly is at&t syntax.
* Dedicated assembly files use intel syntax.
* This is required because of MacOS.
*/

static inline uint8_t inb(uint16_t port) {
uint8_t ret;

// ret takes the value from al, port is put into dx
__asm__ volatile("inb %0, %w1" : "=a"(ret) : "Nd"(port));
__asm__ volatile("inb %w1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}

static inline uint16_t inw(uint16_t port) {
uint16_t ret;

// ret takes the value from ax, port is put into dx
__asm__ volatile("inw %0, %w1" : "=a"(ret) : "Nd"(port));
__asm__ volatile("inw %w1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}

static inline void outb(uint16_t port, uint8_t val) {
// val is put into al, port is put into dx
__asm__ volatile("outb %w1, %0" : : "a"(val), "Nd"(port));
__asm__ volatile("outb %0, %w1" : : "a"(val), "Nd"(port));
}

static inline void outw(uint16_t port, uint16_t val) {
// val is put into ax, port is put into dx
__asm__ volatile("outw %w1, %0" : : "a"(val), "Nd"(port));
__asm__ volatile("outw %0, %w1" : : "a"(val), "Nd"(port));
}

static inline void port_io_wait(void) {
Expand Down
12 changes: 4 additions & 8 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
CC:=gcc
CFLAGS:=-Wall -Werror -Os -Wl,--oformat=binary -no-pie -m32 -s -falign-functions=4 -ffreestanding -masm=intel -fno-asynchronous-unwind-tables -I./../src/lib/
LFLAGS:=-melf_i386 --build-id=none

C_FILES := $(shell find ./src/ -name '*.c')
BIN_NAMES := $(C_FILES:%.c=%.bin)
OBJ_NAMES := $(C_FILES:%.c=%.o)

MOS_ELF :=./../mOS.elf
MOS_ELF := ./../mOS.elf

.PHONY: clean test

all: $(MOS_ELF) $(BIN_NAMES)

%.bin: %.o test_entry.o
ld -o $@.elf $(LFLAGS) -T link.ld $^ --just-symbols=$(MOS_ELF)
objcopy -O binary $@.elf $@
$(LD) -o $@.elf $(LFLAGS) -T link.ld $^ --just-symbols=$(MOS_ELF)
$(OBJCOPY) -O binary $@.elf $@
rm $@.elf

%.o: %.c
gcc -c $^ -o $@ $(CFLAGS)
$(CC) -c $^ -o $@ $(CFLAGS) -I./../src/lib/

test_entry.o: test_entry.asm
nasm $^ -f elf32 -o $@
Expand Down

0 comments on commit f76094b

Please sign in to comment.