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

Version 0.1 #1

Open
wants to merge 18 commits into
base: master
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 .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

steps:
- name: install compiler & emulator
run: apt update && apt install g++-aarch64-linux-gnu qemu-system-arm
run: sudo apt-get update && sudo apt-get install g++-aarch64-linux-gnu qemu-system-arm
- uses: actions/checkout@v2
- name: run tests
run: make test
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bin/
obj/
.vscode/
.vscode/
tags
ctags
80 changes: 80 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
DEBUG := 1

MACHINE_BASE = raspi
MACHINE_VERSION = 3
MACHINE = $(MACHINE_BASE)$(MACHINE_VERSION)

KERNEL_ELF = bin/porpoise.elf
KERNEL_DEBUG = bin/porpoise.debug
KERNEL_BINARY = bin/sdcard/boot/kernel8.img

PREFIX := aarch64-linux-gnu

CXX = $(PREFIX)-g++
INCLUDES = -Iinclude/shared -Iinclude/$(MACHINE_BASE) -Iinclude/shared/lib -Iinclude/$(MACHINE_BASE)/lib
CXFLAGS = $(INCLUDES)\
-D__$(MACHINE_BASE)__=$(MACHINE_VERSION)\
-ffreestanding -nostdlib -fno-exceptions -fno-rtti -fno-stack-protector\
-std=c++17 -Wall -Wextra
LD = $(PREFIX)-ld
LDFLAGS = -Ttools/ld/$(MACHINE).ld
LIBRARIES = $(shell $(CXX) $(CXFLAGS) --print-file-name=libgcc.a)
OBJCOPY = $(PREFIX)-objcopy

EMU = qemu-system-aarch64
EMUFLAGS = -M $(MACHINE),usb=on -smp 4 -m 1G -d guest_errors -display none

OBJECTS = $(patsubst src/%.S,obj/%.o,$(patsubst src/%.cpp,obj/%.o,$(shell find src/shared \( -name '*.cpp' -o -name '*.S' \))))\
$(patsubst src/%.S,obj/%.o,$(patsubst src/%.cpp,obj/%.o,$(shell find src/$(MACHINE_BASE) \( -name '*.cpp' -o -name '*.S' \))))

ifeq ($(DEBUG),)
DEBUG=1
endif

ifeq ($(DEBUG),1)
CXFLAGS += -ggdb -O0
else
CXFLAGS += -g0 -O3
endif

obj/%.o: src/%.cpp
@echo "<<< `basename $^`"
@mkdir -p `dirname $@`
@$(CXX) $(CXFLAGS) -S -o [email protected] $^
@$(CXX) $(CXFLAGS) -c -o $@ [email protected]

obj/%.o: src/%.S
@echo "<<< `basename $^`"
@mkdir -p `dirname $@`
@$(CXX) $(CXFLAGS) -D__ASM_SOURCE__ -c -o $@ $^

all: $(KERNEL_BINARY)

clean:
@rm -rf obj/* $(KERNEL_BINARY) $(KERNEL_ELF) $(KERNEL_DEBUG)

$(KERNEL_BINARY): $(OBJECTS)
@mkdir -p `dirname $@`
@echo ">>> `basename $(KERNEL_ELF)`"
@$(LD) $(LDFLAGS) -o $(KERNEL_ELF) $^ $(LIBRARIES)
@
@echo ">>> `basename $(KERNEL_DEBUG)`"
@cp $(KERNEL_ELF) $(KERNEL_DEBUG)
@$(OBJCOPY) --strip-all $(KERNEL_ELF)
@
@echo ">>> `basename $@`"
@$(OBJCOPY) -O binary $(KERNEL_ELF) $@

run-qemu: $(KERNEL_BINARY)
$(EMU) $(EMUFLAGS) -serial stdio -kernel $(KERNEL_ELF)

mon-qemu: $(KERNEL_BINARY)
$(EMU) $(EMUFLAGS) -monitor stdio -kernel $(KERNEL_ELF)

debug-qemu: $(KERNEL_BINARY)
$(EMU) $(EMUFLAGS) -kernel $(KERNEL_ELF) -s -S -monitor stdio

check:
@cppcheck --quiet --enable=all $(INCLUDES) `find src -name "*.cpp"` `find include -name "*.hpp"` 2>&1 | grep -v "never used"

.PHONY: run-qemu debug-qemu $(KERNEL_BINARY)
16 changes: 16 additions & 0 deletions include/raspi/porpoise/boot-platform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#if __raspi__ >= 3
# define BOOT_ORIGIN 0x80000
#else
# define BOOT_ORIGIN 0x8000
#endif

#ifndef __ASM_SOURCE__
#include <stdint.h>

namespace porpoise { namespace boot {
extern "C" void boot_kernel(uint64_t dtb, uint64_t id) __attribute__((noreturn));
}} // porpoise::boot

#endif // ! __ASM_SOURCE__
8 changes: 8 additions & 0 deletions include/raspi/porpoise/io/mailbox.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stdint.h>

namespace porpoise { namespace io {
struct mailbox {
};
}} // porpoise::io
53 changes: 53 additions & 0 deletions include/raspi/porpoise/io/mmio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <stdint.h>

namespace porpoise { namespace io {
struct mmio
{
#if __raspi__ == 0 || __raspi__ == 1
static constexpr uintptr_t MMIO_BASE = 0x20000000;
#elif __raspi__ == 2 || __raspi__ == 3
static constexpr uintptr_t MMIO_BASE = 0x3F000000;
#elif __raspi__ == 4
static constexpr uintptr_t MMIO_BASE = 0xFE000000;
#else
#error Unknown/undefined Raspberry Pi version
#endif
static constexpr uintptr_t GPIO_BASE = MMIO_BASE + 0x200000;
static constexpr uintptr_t UART0_BASE = GPIO_BASE + 0x1000;
static constexpr uintptr_t MAILBOX_BASE = MMIO_BASE + 0xB880;
enum class reg : uintptr_t
{
GPIO_UP_DOWN = GPIO_BASE + 0x94,
GPIO_CLOCK0 = GPIO_BASE + 0x98,
UART0_DR = UART0_BASE + 0x00,
UART0_RSRECR = UART0_BASE + 0x04,
UART0_FR = UART0_BASE + 0x18,
UART0_ILPR = UART0_BASE + 0x20,
UART0_IBRD = UART0_BASE + 0x24,
UART0_FBRD = UART0_BASE + 0x28,
UART0_LCRH = UART0_BASE + 0x2C,
UART0_CR = UART0_BASE + 0x30,
UART0_IFLS = UART0_BASE + 0x34,
UART0_IMSC = UART0_BASE + 0x38,
UART0_RIS = UART0_BASE + 0x3C,
UART0_MIS = UART0_BASE + 0x40,
UART0_ICR = UART0_BASE + 0x44,
UART0_DMACR = UART0_BASE + 0x48,
UART0_ITCR = UART0_BASE + 0x80,
UART0_ITIP = UART0_BASE + 0x84,
UART0_ITOP = UART0_BASE + 0x88,
UART0_TDR = UART0_BASE + 0x8C,
MAILBOX_READ = MAILBOX_BASE + 0x00,
MAILBOX_STATUS = MAILBOX_BASE + 0x18,
MAILBOX_WRITE = MAILBOX_BASE + 0x20
};

static void put(reg r, uint32_t value);

static uint32_t get(reg r);
private:
mmio() = delete;
};
}} // porpose::io
11 changes: 11 additions & 0 deletions include/shared/lib/move.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

template <class T> struct remove_reference {typedef T type;};
template <class T> struct remove_reference<T&> {typedef T type;};
template <class T> struct remove_reference<T&&> {typedef T type;};

template <class T>
typename remove_reference<T>::type&& move(T&& t)
{
return static_cast<typename remove_reference<T>::type&&>(t);
}
6 changes: 6 additions & 0 deletions include/shared/lib/string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include <stdint.h>
#include <stddef.h>

extern "C" size_t strlen(const char* s);
5 changes: 5 additions & 0 deletions include/shared/porpoise.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <porpoise/abort.hpp>

#define PORPOISE_UNUSED(X) ((void)(X))
16 changes: 16 additions & 0 deletions include/shared/porpoise/abort.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <porpoise/io/logging.hpp>

#define PORPOISE_ABORT(...) \
do \
{ \
auto __log = ::porpoise::io::logging::log::error();\
__log << __VA_ARGS__; \
::porpoise::abort(); \
} while (0)

namespace porpoise
{
extern "C" void abort() __attribute__((noreturn));
}
15 changes: 15 additions & 0 deletions include/shared/porpoise/boot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once


#include <porpoise/boot-platform.hpp>

#ifndef __ASM_SOURCE__
#include <stdint.h>

namespace porpoise { namespace boot {
void start_cpu(int id, void(* fn)());

void cpu_main(int id);
}} // porpoise::boot

#endif // ! __ASM_SOURCE__
48 changes: 48 additions & 0 deletions include/shared/porpoise/heap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <stdint.h>
#include <stddef.h>

#include <porpoise/sync/spinlock.hpp>

namespace porpoise {
struct heap
{
enum class oom_behaviour
{
abort,
return_null,
default_ = abort
};

static constexpr size_t DEFAULT_ALIGNMENT = sizeof(uint64_t);

static void init();

static void* allocate(size_t bytes);

static void* allocate(
size_t bytes,
size_t alignment
);

static void* allocate(
size_t bytes,
oom_behaviour behaviour
);

static void* allocate(
size_t bytes,
size_t alignment,
oom_behaviour behaviour
);

static void deallocate(void* ptr);
private:
static constexpr uintptr_t TOP = 0x3b3fffff;
static uintptr_t _curr;
static sync::spinlock _lock;

heap() = delete;
};
}
4 changes: 4 additions & 0 deletions include/shared/porpoise/io/logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#include <porpoise/io/logging/log.hpp>
#include <porpoise/io/logging/manipulators.hpp>
Loading