diff --git a/.gitignore b/.gitignore index d25af32d..cb75e7f2 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ dump .cache/* compile_commands.json .clangd +*.d diff --git a/Makefile b/Makefile index 282b54e6..3255c719 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,4 @@ -ARCH ?= aarch64-none-elf -CC := $(ARCH)-gcc -LD := $(ARCH)-ld -AR := $(ARCH)-ar -OBJCOPY := $(ARCH)-objcopy - -CFLAGS_BASE ?= -g -O0 -nostdlib -ffreestanding \ - -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables \ - -Wall -Wextra -Wno-unused-parameter -Wno-address-of-packed-member -mcpu=cortex-a72 -CONLY_FLAGS_BASE ?= -std=c17 -LDFLAGS_BASE ?= - -LOAD_ADDR ?= 0x41000000 -XHCI_CTX_SIZE ?= 32 -QEMU ?= true -MODE ?= virt - -export ARCH CC LD AR OBJCOPY CFLAGS_BASE CONLY_FLAGS_BASE LDFLAGS_BASE LOAD_ADDR XHCI_CTX_SIZE QEMU +include common.mk OS := $(shell uname) FS_DIRS := fs/redos/user @@ -42,13 +25,13 @@ kernel: $(MAKE) -C kernel LOAD_ADDR=$(LOAD_ADDR) XHCI_CTX_SIZE=$(XHCI_CTX_SIZE) QEMU=$(QEMU) clean: - $(MAKE) -C shared clean - $(MAKE) -C user clean - $(MAKE) -C kernel clean + $(MAKE) -C shared $@ + $(MAKE) -C user $@ + $(MAKE) -C kernel $@ @echo "removing fs dirs" - rm -rf $(FS_DIRS) + $(RM) -r $(FS_DIRS) @echo "removing images" - rm -f kernel.img kernel.elf disk.img dump + $(RM) kernel.img kernel.elf disk.img dump raspi: $(MAKE) LOAD_ADDR=0x80000 XHCI_CTX_SIZE=64 QEMU=true all @@ -63,11 +46,11 @@ run: debug: $(MAKE) $(MODE) ./rundebug MODE=$(MODE) $(ARGS) - + dump: $(OBJCOPY) -O binary kernel.elf kernel.img - aarch64-none-elf-objdump -D kernel.elf > dump - + $(ARCH)-objdump -D kernel.elf > dump + install: $(MAKE) clean $(MAKE) LOAD_ADDR=0x80000 XHCI_CTX_SIZE=64 QEMU=false all @@ -89,4 +72,6 @@ help: make debug build and run with debugger\n\ make dump disassemble kernel.elf\n\ make install create raspi kernel and mount it on a bootable partition\n\ - make prepare-fs create directories for the filesystem\n\n" + make prepare-fs create directories for the filesystem\n\n"\ + \n\ + Use 'make V=1' for verbose build output. diff --git a/common.mk b/common.mk new file mode 100644 index 00000000..676b839e --- /dev/null +++ b/common.mk @@ -0,0 +1,34 @@ +ARCH ?= aarch64-none-elf +CC := $(ARCH)-gcc +CXX := $(ARCH)-g++ +LD := $(ARCH)-ld +AR := $(ARCH)-ar +OBJCOPY := $(ARCH)-objcopy + +COMMON_FLAGS ?= -ffreestanding -nostdlib -fno-exceptions -fno-unwind-tables \ + -fno-asynchronous-unwind-tables -g -O0 -Wall -Wextra \ + -Wno-unused-parameter -Wno-address-of-packed-member \ + -mcpu=cortex-a72 + +CFLAGS_BASE ?= $(COMMON_FLAGS) -std=c17 +CXXFLAGS_BASE ?= $(COMMON_FLAGS) -fno-rtti +LDFLAGS_BASE ?= + +LOAD_ADDR ?= 0x41000000 +XHCI_CTX_SIZE ?= 32 +QEMU ?= true +MODE ?= virt + +ifeq ($(V), 1) + VAR = $(AR) + VAS = $(CC) + VCC = $(CC) + VCXX = $(CXX) + VLD = $(LD) +else + VAR = @echo " [AR] $@" && $(AR) + VAS = @echo " [AS] $@" && $(CC) + VCC = @echo " [CC] $@" && $(CC) + VCXX = @echo " [CXX] $@" && $(CXX) + VLD = @echo " [LD] $@" && $(LD) +endif diff --git a/kernel/Makefile b/kernel/Makefile index b50e98e6..db1c553c 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,19 +1,26 @@ #kernel # toolchain (inherited from top-level) -CFLAGS := $(CFLAGS_BASE) -I. -I../shared -I../user -DXHCI_CTX_SIZE=$(XHCI_CTX_SIZE) +include ../common.mk + +CPPFLAGS := -I. -I../shared -I../user -DXHCI_CTX_SIZE=$(XHCI_CTX_SIZE) + ifeq ($(QEMU),true) - CFLAGS += -DQEMU + CPPFLAGS += -DQEMU endif -LDFLAGS := $(LDFLAGS_BASE) -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) +CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) +CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) +LDFLAGS := $(LDFLAGS_BASE) -T $(shell ls *.ld) --defsym=LOAD_ADDR=$(LOAD_ADDR) CLEAN_OBJS := $(shell find . -name '*.o') +CLEAN_DEPS := $(shell find . -name '*.d') C_SRC := $(shell find . -name '*.c') ASM_SRC := $(shell find . -name '*.S') CPP_SRC := $(shell find . -name '*.cpp') OBJ := $(C_SRC:.c=.o) $(ASM_SRC:.S=.o) $(CPP_SRC:.cpp=.o) OBJL := $(filter-out ./boot.o,$(OBJ)) +DEP := $(C_SRC:.c=.d) $(ASM_SRC:.S=.d) $(CPP_SRC:.cpp=.d) ELF := ../kernel.elf TARGET := ../kernel.img @@ -21,15 +28,19 @@ TARGET := ../kernel.img all: $(TARGET) $(TARGET): ../shared/libshared.a $(OBJ) - $(LD) $(LDFLAGS) -o $(ELF) $(OBJL) ../shared/libshared.a + $(VLD) $(LDFLAGS) -o $(ELF) $(OBJL) ../shared/libshared.a $(OBJCOPY) -O binary $(ELF) $@ %.o: %.S - $(CC) $(CFLAGS) -c $< -o $@ + $(VAS) $(CFLAGS) -c $< -o $@ + %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c $< -o $@ + $(VCC) $(CFLAGS) -c -MMD -MP $< -o $@ + %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ + $(VCXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(ELF) $(TARGET) + $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) + +-include $(DEP) diff --git a/kernel/audio/virtio_audio_pci.cpp b/kernel/audio/virtio_audio_pci.cpp index 7f77991c..f5850dea 100644 --- a/kernel/audio/virtio_audio_pci.cpp +++ b/kernel/audio/virtio_audio_pci.cpp @@ -6,10 +6,10 @@ #include "audio.h" #define VIRTIO_SND_R_PCM_INFO 0x0100 -#define VIRTIO_SND_R_PCM_SET_PARAMS 0x0101 -#define VIRTIO_SND_R_PCM_PREPARE 0x0102 -#define VIRTIO_SND_R_PCM_RELEASE 0x0103 -#define VIRTIO_SND_R_PCM_START 0x0104 +#define VIRTIO_SND_R_PCM_SET_PARAMS 0x0101 +#define VIRTIO_SND_R_PCM_PREPARE 0x0102 +#define VIRTIO_SND_R_PCM_RELEASE 0x0103 +#define VIRTIO_SND_R_PCM_START 0x0104 #define VIRTIO_SND_R_PCM_STOP 0x0105 #define VIRTIO_SND_S_OK 0x8000 @@ -17,7 +17,7 @@ #define VIRTIO_SND_S_NOT_SUPP 0x8002 #define VIRTIO_SND_S_IO_ERR 0x8003 -#define VIRTIO_SND_PCM_FMT_U32 18 +#define VIRTIO_SND_PCM_FMT_U32 18 #define VIRTIO_SND_PCM_FMT_FLOAT 19 #define VIRTIO_SND_PCM_FMT_FLOAT64 20 @@ -38,49 +38,49 @@ #define VIRTIO_SND_D_OUTPUT 0 #define VIRTIO_SND_D_INPUT 1 -typedef struct virtio_snd_hdr { - uint32_t code; -} virtio_snd_hdr; +typedef struct virtio_snd_hdr { + uint32_t code; +} virtio_snd_hdr; static_assert(sizeof(virtio_snd_hdr) == 4, "Sound header must be 4 bytes"); -typedef struct virtio_snd_query_info { - virtio_snd_hdr hdr; - uint32_t start_id; - uint32_t count; - uint32_t size; +typedef struct virtio_snd_query_info { + virtio_snd_hdr hdr; + uint32_t start_id; + uint32_t count; + uint32_t size; } virtio_snd_query_info; static_assert(sizeof(virtio_snd_query_info) == 16, "Query info struct must be 16 bytes"); -typedef struct virtio_snd_pcm_hdr { - virtio_snd_hdr hdr; - uint32_t stream_id; -} virtio_snd_pcm_hdr; +typedef struct virtio_snd_pcm_hdr { + virtio_snd_hdr hdr; + uint32_t stream_id; +} virtio_snd_pcm_hdr; -typedef struct virtio_snd_info_hdr { - uint32_t hda_fn_nid; +typedef struct virtio_snd_info_hdr { + uint32_t hda_fn_nid; } virtio_snd_info_hdr; -typedef struct virtio_snd_pcm_info { - virtio_snd_info_hdr info_hdr; - uint32_t features; /* 1 << VIRTIO_SND_PCM_F_XXX */ - uint64_t formats; /* 1 << VIRTIO_SND_PCM_FMT_XXX */ - uint64_t rates; /* 1 << VIRTIO_SND_PCM_RATE_XXX */ - uint8_t direction; - uint8_t channels_min; - uint8_t channels_max; - - uint8_t padding[5]; +typedef struct virtio_snd_pcm_info { + virtio_snd_info_hdr info_hdr; + uint32_t features; /* 1 << VIRTIO_SND_PCM_F_XXX */ + uint64_t formats; /* 1 << VIRTIO_SND_PCM_FMT_XXX */ + uint64_t rates; /* 1 << VIRTIO_SND_PCM_RATE_XXX */ + uint8_t direction; + uint8_t channels_min; + uint8_t channels_max; + + uint8_t padding[5]; }__attribute__((packed)) virtio_snd_pcm_info; static_assert(sizeof(virtio_snd_pcm_info) == 32, "PCM Info must be 32"); -typedef struct virtio_snd_event { - struct virtio_snd_hdr hdr; - uint32_t data; +typedef struct virtio_snd_event { + struct virtio_snd_hdr hdr; + uint32_t data; }__attribute__((packed)) virtio_snd_event; bool VirtioAudioDriver::init(){ uint64_t addr = find_pci_device(VIRTIO_VENDOR, VIRTIO_AUDIO_ID); - if (!addr){ + if (!addr){ kprintf("Disk device not found"); return false; } @@ -142,14 +142,14 @@ void VirtioAudioDriver::config_jacks(){ } -typedef struct virtio_snd_pcm_xfer { - uint32_t stream_id; -}__attribute__((packed)) virtio_snd_pcm_xfer; - -typedef struct virtio_snd_pcm_status { - uint32_t status; - uint32_t latency_bytes; -}__attribute__((packed)) virtio_snd_pcm_status; +typedef struct virtio_snd_pcm_xfer { + uint32_t stream_id; +}__attribute__((packed)) virtio_snd_pcm_xfer; + +typedef struct virtio_snd_pcm_status { + uint32_t status; + uint32_t latency_bytes; +}__attribute__((packed)) virtio_snd_pcm_status; bool VirtioAudioDriver::config_streams(uint32_t streams){ virtio_snd_query_info* cmd = (virtio_snd_query_info*)kalloc(audio_dev.memory_page, sizeof(virtio_snd_query_info), ALIGN_4KB, true, true); @@ -159,7 +159,7 @@ bool VirtioAudioDriver::config_streams(uint32_t streams){ cmd->size = sizeof(virtio_snd_pcm_info); size_t resp_size = sizeof(virtio_snd_hdr) + (streams * cmd->size); - + uintptr_t resp = (uintptr_t)kalloc(audio_dev.memory_page, resp_size, ALIGN_64B, true, true); if (!virtio_send(&audio_dev, audio_dev.common_cfg->queue_desc, audio_dev.common_cfg->queue_driver, audio_dev.common_cfg->queue_device, @@ -170,7 +170,7 @@ bool VirtioAudioDriver::config_streams(uint32_t streams){ } uint8_t *streams_bytes = (uint8_t*)(resp + sizeof(virtio_snd_hdr)); - + virtio_snd_pcm_info *stream_info = (virtio_snd_pcm_info*)streams_bytes; for (uint32_t stream = 0; stream < streams; stream++){ uint64_t format = read_unaligned64(&stream_info[stream].formats); @@ -198,20 +198,21 @@ bool VirtioAudioDriver::config_streams(uint32_t streams){ if (stream_info[stream].direction == VIRTIO_SND_D_OUTPUT){ kprintf("Playing from stream %i",stream); select_queue(&audio_dev, TRANSMIT_QUEUE); - + for (uint16_t i = 0; i < 100; i++){ size_t total_size = sizeof(virtio_snd_pcm_status) + sizeof(virtio_snd_pcm_xfer) + TOTAL_BUF_SIZE; uintptr_t full_buffer = (uintptr_t)kalloc(audio_dev.memory_page, total_size, ALIGN_4KB, true, true); virtio_snd_pcm_xfer *header = (virtio_snd_pcm_xfer*)full_buffer; header->stream_id = stream; - + uint32_t *buf = (uint32_t*)(full_buffer + sizeof(virtio_snd_pcm_xfer)); uint32_t buf_size = TOTAL_BUF_SIZE/SND_U32_BYTES; for (uint32_t sample = 0; sample < buf_size; sample++){ + // TODO: what is meant here? GCC suggests parentheses buf[sample] = sample < buf_size/2 == 0 ? 0x88888888 : UINT32_MAX; } - + virtio_send_1d(&audio_dev, full_buffer, total_size); } @@ -223,16 +224,16 @@ bool VirtioAudioDriver::config_streams(uint32_t streams){ return true; } -typedef struct virtio_snd_pcm_set_params { +typedef struct virtio_snd_pcm_set_params { virtio_snd_pcm_hdr hdr; - uint32_t buffer_bytes; - uint32_t period_bytes; - uint32_t features; - uint8_t channels; - uint8_t format; - uint8_t rate; - - uint8_t padding; + uint32_t buffer_bytes; + uint32_t period_bytes; + uint32_t features; + uint8_t channels; + uint8_t format; + uint8_t rate; + + uint8_t padding; }__attribute__((packed)) virtio_snd_pcm_set_params; static_assert(sizeof(virtio_snd_pcm_set_params) == 24, "Virtio sound Set Params command needs to be n bytes"); @@ -252,7 +253,7 @@ bool VirtioAudioDriver::stream_set_params(uint32_t stream_id, uint32_t features, bool result = virtio_send(&audio_dev, audio_dev.common_cfg->queue_desc, audio_dev.common_cfg->queue_driver, audio_dev.common_cfg->queue_device, (uintptr_t)cmd, sizeof(virtio_snd_pcm_set_params), (uintptr_t)resp, sizeof(virtio_snd_info_hdr), VIRTQ_DESC_F_WRITE); - + kfree(cmd, sizeof(virtio_snd_query_info)); kfree((void*)resp, sizeof(virtio_snd_info_hdr)); @@ -261,7 +262,7 @@ bool VirtioAudioDriver::stream_set_params(uint32_t stream_id, uint32_t features, if (result) result = send_simple_stream_cmd(stream_id, VIRTIO_SND_R_PCM_START); - + return result; } @@ -270,15 +271,15 @@ bool VirtioAudioDriver::send_simple_stream_cmd(uint32_t stream_id, uint32_t comm virtio_snd_pcm_hdr* cmd = (virtio_snd_pcm_hdr*)kalloc(audio_dev.memory_page, sizeof(virtio_snd_pcm_hdr), ALIGN_4KB, true, true); cmd->hdr.code = command; cmd->stream_id = stream_id; - + virtio_snd_info_hdr *resp = (virtio_snd_info_hdr*)kalloc(audio_dev.memory_page, sizeof(virtio_snd_info_hdr), ALIGN_64B, true, true); - + bool result = virtio_send(&audio_dev, audio_dev.common_cfg->queue_desc, audio_dev.common_cfg->queue_driver, audio_dev.common_cfg->queue_device, (uintptr_t)cmd, sizeof(virtio_snd_pcm_hdr), (uintptr_t)resp, sizeof(virtio_snd_info_hdr), VIRTQ_DESC_F_WRITE); kfree(cmd, sizeof(virtio_snd_query_info)); kfree((void*)resp, sizeof(virtio_snd_info_hdr)); - + return result; } @@ -308,4 +309,4 @@ void VirtioAudioDriver::handle_interrupt(){ *(volatile uint16_t*)(uintptr_t)(audio_dev.notify_cfg + audio_dev.notify_off_multiplier * EVENT_QUEUE) = 0; } select_queue(&audio_dev, CONTROL_QUEUE); -} \ No newline at end of file +} diff --git a/kernel/console/kio.c b/kernel/console/kio.c index 17b3bc26..b6edf458 100644 --- a/kernel/console/kio.c +++ b/kernel/console/kio.c @@ -27,6 +27,7 @@ size_t console_read(file *fd, char *out_buf, size_t size, file_offset offset){ size_t console_write(file *fd, const char *buf, size_t size, file_offset offset){ kprintf(buf); + return size; } @@ -72,7 +73,7 @@ void kprintf(const char *fmt, ...){ va_list args; va_start(args, fmt); char* buf = kalloc(print_buf, 256, ALIGN_64B, true, false); - size_t len = string_format_va_buf(fmt, buf, args); + string_format_va_buf(fmt, buf, args); va_end(args); puts(buf); putc('\r'); @@ -92,7 +93,7 @@ void kputf(const char *fmt, ...){ va_list args; va_start(args, fmt); char* buf = kalloc(print_buf, 256, ALIGN_64B, true, false); - size_t len = string_format_va_buf(fmt, buf, args); + string_format_va_buf(fmt, buf, args); va_end(args); puts(buf); // kfree((void*)buf, 256); @@ -104,4 +105,4 @@ void disable_visual(){ void enable_visual(){ use_visual = true; -} \ No newline at end of file +} diff --git a/kernel/input/dwc2.cpp b/kernel/input/dwc2.cpp index 3394ba1e..3b37ee1a 100644 --- a/kernel/input/dwc2.cpp +++ b/kernel/input/dwc2.cpp @@ -57,7 +57,7 @@ bool DWC2Driver::init() { //Device setup0 host->port |= (1 << 12); - + if (!wait(&host->port, 1, true, 2000)){ kprintf("[DWC2] No device connected %x",host->port); return true; @@ -69,7 +69,7 @@ bool DWC2Driver::init() { } channel_map = IndexMap(127 * 5); - usb_manager = new USBManager(127); + usb_manager = new USBManager(127); kprintf("Port reset %x",host->port); @@ -107,10 +107,10 @@ bool DWC2Driver::make_transfer(dwc2_host_channel *channel, bool in, uint8_t pid, channel->cchar &= ~(1 << 15); channel->cchar |= ((in ? 1 : 0) << 15); - + channel->cchar &= ~(1 << 30); channel->cchar &= ~(1 << 31); - channel->cchar |= (1 << 31); + channel->cchar |= (1 << 31); if (!wait(&channel->interrupt, 1, true, 2000)){ kprintf("[DWC2 error] Transfer timed out."); @@ -121,11 +121,11 @@ bool DWC2Driver::make_transfer(dwc2_host_channel *channel, bool in, uint8_t pid, } bool DWC2Driver::request_sized_descriptor(uint8_t address, uint8_t endpoint, uint8_t rType, uint8_t request, uint8_t type, uint16_t descriptor_index, uint16_t wIndex, uint16_t descriptor_size, void *out_descriptor){ - + usb_setup_packet packet = { .bmRequestType = rType, .bRequest = request, - .wValue = (type << 8) | descriptor_index, + .wValue = (uint16_t)((type << 8) | descriptor_index), .wIndex = wIndex, .wLength = descriptor_size }; @@ -156,26 +156,26 @@ bool DWC2Driver::request_sized_descriptor(uint8_t address, uint8_t endpoint, uin } bool DWC2Driver::configure_endpoint(uint8_t address, usb_endpoint_descriptor *endpoint, uint8_t configuration_value, usb_device_types type){ - + uint8_t ep_address = endpoint->bEndpointAddress; uint8_t ep_num = ep_address & 0x0F; uint8_t ep_dir = (ep_address & 0x80) >> 7; - + uint8_t ep_type = endpoint->bmAttributes & 0x03; // 0 = Control, 1 = Iso, 2 = Bulk, 3 = Interrupt - + kprintf("[DWC2] endpoint %i info. Direction %i type %i",ep_num, ep_dir, ep_type); - + //Configure endpoint request_sized_descriptor(address, 0, 0x00, 0x09, 0, configuration_value, 0, 0, 0); - + uint8_t conf; request_sized_descriptor(address, 0, 0x80, 0x08, 0, 0, 0, 1, &conf); - + if (!conf){ kprintf("Failed to set configuration for device"); return false; } - + dwc2_host_channel *channel = get_channel(channel_map[address << 8]); endpoint_channel = get_channel(assign_channel(address, ep_num, ep_type)); if (channel->splt) @@ -212,11 +212,11 @@ bool DWC2Driver::poll(uint8_t address, uint8_t endpoint, void *out_buf, uint16_t endpoint_channel->intmask = 0xFFFFFFFF; endpoint_channel->interrupt = 0xFFFFFFFF; - + endpoint_channel->cchar &= ~(1 << 30); endpoint_channel->cchar &= ~(1 << 31); - endpoint_channel->cchar |= (1 << 31); + endpoint_channel->cchar |= (1 << 31); if (!wait(&endpoint_channel->interrupt, 1, true, 10)){ return false; @@ -235,4 +235,4 @@ void DWC2Driver::handle_hub_routing(uint8_t hub, uint8_t port){ dev_channel->splt = (1 << 31) | (1 << 16) | (hub << 7) | (port << 0); } -void DWC2Driver::handle_interrupt(){} \ No newline at end of file +void DWC2Driver::handle_interrupt(){} diff --git a/kernel/input/input_dispatch.cpp b/kernel/input/input_dispatch.cpp index 52db6ef2..5e6a0973 100644 --- a/kernel/input/input_dispatch.cpp +++ b/kernel/input/input_dispatch.cpp @@ -107,12 +107,12 @@ bool sys_shortcut_triggered(uint16_t pid, uint16_t sid){ if (shortcuts[sid].pid == pid && shortcuts[sid].triggered){ shortcuts[sid].triggered = false; return true; - } + } return false; } bool input_init(){ - for (int i = 0; i < 16; i++) shortcuts[i] = (shortcut){0}; + for (int i = 0; i < 16; i++) shortcuts[i] = {}; if (BOARD_TYPE == 2 && RPI_BOARD != 5){ input_driver = new DWC2Driver();//TODO: QEMU & 3 Only return input_driver->init(); @@ -156,4 +156,4 @@ driver_module input_module = (driver_module){ .write = 0, .seek = 0, .readdir = 0, -}; \ No newline at end of file +}; diff --git a/kernel/input/usb.cpp b/kernel/input/usb.cpp index 9455ac5f..ef35231d 100644 --- a/kernel/input/usb.cpp +++ b/kernel/input/usb.cpp @@ -24,7 +24,7 @@ bool USBDriver::setup_device(uint8_t address, uint16_t port){ return false; } usb_device_descriptor* descriptor = (usb_device_descriptor*)kalloc(mem_page, sizeof(usb_device_descriptor), ALIGN_64B, true, true); - + if (!request_descriptor(address, 0, 0x80, 6, USB_DEVICE_DESCRIPTOR, 0, 0, descriptor)){ kprintf("[USB error] failed to get device descriptor"); return false; @@ -104,7 +104,7 @@ bool USBDriver::get_configuration(uint8_t address){ uint8_t* report_descriptor; uint16_t report_length; - usb_device_types dev_type; + usb_device_types dev_type = UNKNOWN; kprintf("[USB] set configuration %i for device %i", config->bConfigurationValue, address); request_sized_descriptor(address, 0, 0, 9, 0, config->bConfigurationValue, 0, 0, 0); @@ -136,7 +136,7 @@ bool USBDriver::get_configuration(uint8_t address){ case 0x1: dev_type = KEYBOARD; break; - + default: dev_type = UNKNOWN; break; @@ -160,7 +160,7 @@ bool USBDriver::get_configuration(uint8_t address){ } case 0x5: {//Endpoint usb_endpoint_descriptor *endpoint = (usb_endpoint_descriptor*)&config->data[i]; - + if (dev_type != UNKNOWN) configure_endpoint(address, endpoint, config->bConfigurationValue, dev_type); @@ -173,7 +173,7 @@ bool USBDriver::get_configuration(uint8_t address){ } return true; - + } bool USBDriver::request_descriptor(uint8_t address, uint8_t endpoint, uint8_t rType, uint8_t request, uint8_t type, uint16_t index, uint16_t wIndex, void *out_descriptor){ @@ -212,4 +212,4 @@ void USBDriver::hub_enumerate(uint8_t address){ void USBDriver::poll_inputs(){ usb_manager->poll_inputs(this); -} \ No newline at end of file +} diff --git a/kernel/input/xhci.cpp b/kernel/input/xhci.cpp index 7bcedafc..9d934468 100644 --- a/kernel/input/xhci.cpp +++ b/kernel/input/xhci.cpp @@ -53,11 +53,11 @@ bool XHCIDriver::check_fatal_error() { #define XHCI_SPEED_SUPER_SPEED 4 #define XHCI_SPEED_SUPER_SPEED_PLUS 5 -#define XHCI_EP_DISABLED 0 +#define XHCI_EP_DISABLED 0 #define XHCI_EP_CONTROL 4 bool XHCIDriver::init(){ - uint64_t addr, mmio, mmio_size; + uint64_t addr = 0, mmio, mmio_size; bool use_pci = false; use_interrupts = true; if (XHCI_BASE){ @@ -69,7 +69,7 @@ bool XHCIDriver::init(){ addr = find_pci_device(0x1B36, 0xD); use_pci = true; } - if (!addr){ + if (!addr){ kprintf("[PCI] xHCI device not found"); return false; } @@ -80,16 +80,16 @@ bool XHCIDriver::init(){ kprintf("[xHCI] Wrong capabilities list"); return false; } - + pci_enable_device(addr); - + if (!pci_setup_bar(addr, 0, &mmio, &mmio_size)){ kprintf("[xHCI] BARs not set up"); return false; } - + pci_register(mmio, mmio_size); - + uint8_t interrupts_ok = pci_setup_interrupts(addr, INPUT_IRQ, 1); switch(interrupts_ok){ case 0: @@ -103,7 +103,7 @@ bool XHCIDriver::init(){ kprintfv("[xHCI] Interrupts setup with MSI %i",INPUT_IRQ); break; } - + kprintfv("[xHCI] BARs set up @ %x (%x)",mmio,mmio_size); } @@ -141,9 +141,9 @@ bool XHCIDriver::init(){ usb_manager = new USBManager(max_device_slots); uint16_t erst_max = ((cap->hcsparams2 >> 4) & 0xF); - + kprintfv("[xHCI] ERST Max: 2^%i",erst_max); - + op->dnctrl = 0xFFFF;//Enable device notifications op->config = max_device_slots; @@ -191,7 +191,7 @@ bool XHCIDriver::init(){ context_map = IndexMap(255 * 5); kprintfv("[xHCI] Init complete with usbcmd %x, usbsts %x",op->usbcmd, op->usbsts); - + if (check_fatal_error()) return false; for (uint16_t i = 0; i < max_ports; i++) @@ -232,7 +232,7 @@ bool XHCIDriver::port_reset(uint16_t port){ //TODO: if usb3 // port_info->portsc.wpr = 1; - //else + //else if (!AWAIT(0, { port_info->portsc.pr = 1; },TRB_TYPE_PORT_STATUS_CHANGE)){ kprintf("[xHCI error] failed port reset"); @@ -267,16 +267,16 @@ bool XHCIDriver::enable_events(){ event_ring.cycle_bit = 1; kprintfv("[xHCI] Interrupter register @ %x", rt_base + 0x20); - + interrupter->erstsz = 1; kprintfv("[xHCI] ERSTSZ set to: %x", interrupter->erstsz); - + interrupter->erdp = ev_ring; interrupter->erstba = erst_addr; kprintfv("[xHCI] ERSTBA set to: %x", interrupter->erstba); - + kprintfv("[xHCI] ERDP set to: %x", interrupter->erdp); - + interrupter->iman |= 1 << 1;//Enable interrupt op->usbsts = 1 << 3;//Enable interrupts @@ -326,7 +326,7 @@ bool XHCIDriver::await_response(uint64_t command, uint32_t type){ } if ((((last_event->control & TRB_TYPE_MASK) >> 10) == type) && (command == 0 || last_event->parameter == command)){ uint8_t completion_code = (last_event->status >> 24) & 0xFF; - if (completion_code != 1) + if (completion_code != 1) kprintf("[xHCI error] wrong status %i on command type %x", completion_code, ((last_event->control & TRB_TYPE_MASK) >> 10) ); interrupter->erdp = (uintptr_t)&event_ring.ring[event_ring.index+1] | (1 << 3);//Inform of latest processed event interrupter->iman |= 1;//Clear interrupts @@ -381,19 +381,19 @@ bool XHCIDriver::setup_device(uint8_t address, uint16_t port){ context_map[address << 8] = ctx; void* output_ctx = (void*)kalloc(mem_page, 0x1000, ALIGN_64B, true, true); kprintfv("[xHCI] Allocating output for context at %x", (uintptr_t)output_ctx); - + ctx->control_context.add_flags = 0b11; - + ctx->device_context.slot_f0.speed = ports[port].portsc.port_speed; ctx->device_context.slot_f0.context_entries = 1; ctx->device_context.slot_f1.root_hub_port_num = port + 1; - + ctx->device_context.endpoints[0].endpoint_f0.endpoint_state = XHCI_EP_DISABLED; ctx->device_context.endpoints[0].endpoint_f1.endpoint_type = XHCI_EP_CONTROL; ctx->device_context.endpoints[0].endpoint_f0.interval = 0; ctx->device_context.endpoints[0].endpoint_f1.error_count = 3; ctx->device_context.endpoints[0].endpoint_f1.max_packet_size = packet_size(ctx->device_context.slot_f0.speed); - + transfer_ring->ring = (trb*)kalloc(mem_page, MAX_TRB_AMOUNT * sizeof(trb), ALIGN_64B, true, true); kprintfv("Transfer ring at %x %i",(uintptr_t)transfer_ring->ring, address << 8); make_ring_link(transfer_ring->ring, transfer_ring->cycle_bit); @@ -411,15 +411,13 @@ bool XHCIDriver::request_sized_descriptor(uint8_t address, uint8_t endpoint, uin usb_setup_packet packet = { .bmRequestType = rType, .bRequest = request, - .wValue = (type << 8) | descriptor_index, + .wValue = (uint16_t)((type << 8) | descriptor_index), .wIndex = wIndex, .wLength = descriptor_size }; // kprintf("RT: %x R: %x V: %x I: %x L: %x",packet.bmRequestType,packet.bRequest,packet.wValue,packet.wIndex,packet.wLength); - bool is_in = (rType & 0x80) != 0; - xhci_ring *transfer_ring = &endpoint_map[address << 8 | endpoint]; trb* setup_trb = &transfer_ring->ring[transfer_ring->index++]; @@ -435,7 +433,7 @@ bool XHCIDriver::request_sized_descriptor(uint8_t address, uint8_t endpoint, uin //bit 16 = direction data->control = (1 << 16) | (TRB_TYPE_DATA_STAGE << 10) | (0 << 4) | transfer_ring->cycle_bit; } - + trb* status_trb = &transfer_ring->ring[transfer_ring->index++]; status_trb->parameter = 0; status_trb->status = 0; @@ -482,7 +480,7 @@ uint32_t XHCIDriver::calculate_interval(uint32_t speed, uint32_t received_interv uint32_t i; for (i = 3; i < 11; i++) - if (125 * (1 << i) >= 1000 * received_interval) break; + if (125u * (1 << i) >= 1000 * received_interval) break; return i; } @@ -495,7 +493,7 @@ bool XHCIDriver::configure_endpoint(uint8_t address, usb_endpoint_descriptor *en uint8_t ep_type = endpoint->bmAttributes & 0x03; // 0 = Control, 1 = Iso, 2 = Bulk, 3 = Interrupt - if (ep_type != 3){ + if (ep_type != 3){ kprintf("[xHCI implementation warning] Endpoint type %i not supported. Ignored",ep_type); return true; } @@ -509,15 +507,15 @@ bool XHCIDriver::configure_endpoint(uint8_t address, usb_endpoint_descriptor *en ctx->device_context.slot_f0.context_entries = ep_num; ctx->device_context.slot_f0.speed = context->slot_f0.speed; ctx->device_context.endpoints[ep_num-1].endpoint_f0.interval = calculate_interval(context->slot_f0.speed, endpoint->bInterval); - + ctx->device_context.endpoints[ep_num-1].endpoint_f0.endpoint_state = XHCI_EP_DISABLED; ctx->device_context.endpoints[ep_num-1].endpoint_f1.endpoint_type = get_ep_type(endpoint); ctx->device_context.endpoints[ep_num-1].endpoint_f1.max_packet_size = endpoint->wMaxPacketSize; ctx->device_context.endpoints[ep_num-1].endpoint_f4.max_esit_payload_lo = endpoint->wMaxPacketSize; ctx->device_context.endpoints[ep_num-1].endpoint_f1.error_count = 3; - + xhci_ring *ep_ring = &endpoint_map[address << 8 | ep_num]; - + ep_ring->ring = (trb*)kalloc(mem_page, MAX_TRB_AMOUNT * sizeof(trb), ALIGN_64B, true, true); ep_ring->cycle_bit = 1; make_ring_link(ep_ring->ring, ep_ring->cycle_bit); @@ -532,7 +530,7 @@ bool XHCIDriver::configure_endpoint(uint8_t address, usb_endpoint_descriptor *en usb_manager->register_endpoint(address, ep_num, type, endpoint->wMaxPacketSize); usb_manager->request_data(address, ep_num, this); - + return true; } @@ -575,10 +573,10 @@ void XHCIDriver::handle_interrupt(){ uint8_t slot_id = (ev->control & TRB_SLOT_MASK) >> 24; uint8_t endpoint_id = (ev->control & TRB_ENDPOINT_MASK) >> 16; kprintfv("Received input from slot %i endpoint %i",slot_id, endpoint_id); - + if (completion_code == 4) usb_manager->request_data(slot_id,endpoint_id,this); - else + else usb_manager->process_data(slot_id,endpoint_id, this); break; } @@ -598,4 +596,4 @@ void XHCIDriver::handle_interrupt(){ interrupter->erdp = (uintptr_t)&event_ring.ring[event_ring.index] | (1 << 3);//Inform of latest processed event interrupter->iman |= 1;//Clear interrupts op->usbsts |= 1 << 3;//Clear interrupts -} \ No newline at end of file +} diff --git a/kernel/kernel_processes/monitor/monitor_processes.c b/kernel/kernel_processes/monitor/monitor_processes.c index bcdb151f..7570bfe7 100644 --- a/kernel/kernel_processes/monitor/monitor_processes.c +++ b/kernel/kernel_processes/monitor/monitor_processes.c @@ -23,7 +23,7 @@ char* parse_proc_state(int state){ case RUNNING: case BLOCKED: return "Running"; - + default: return "Invalid"; } @@ -96,7 +96,7 @@ void draw_process_view(){ int index = scroll_index; int valid_count = 0; - process_t *proc; + process_t *proc = NULL; while (index < MAX_PROCS) { proc = &processes[index]; if (proc->id != 0 && proc->state != STOPPED) { @@ -108,6 +108,8 @@ void draw_process_view(){ index++; } + if (!proc) continue; // TODO: panic? + if (proc->id == 0 || valid_count < i || proc->state == STOPPED) break; string name = string_l((const char*)(uintptr_t)proc->name); @@ -127,11 +129,11 @@ void draw_process_view(){ gpu_draw_string(name, (gpu_point){xo, name_y}, scale, BG_COLOR); gpu_draw_string(state, (gpu_point){xo, state_y}, scale, BG_COLOR); - + string pc = string_from_hex(proc->pc); gpu_draw_string(pc, (gpu_point){xo, pc_y}, scale, BG_COLOR); free(pc.data, pc.mem_length); - + draw_memory("Stack", xo, stack_y, stack_width, stack_height, proc->stack - proc->sp, proc->stack_size); uint64_t heap = calc_heap(proc->heap); uint64_t heap_limit = ((heap + 0xFFF) & ~0xFFF); @@ -160,7 +162,7 @@ void monitor_procs(){ if (sys_shortcut_triggered_current(shortcut)){ if (active) pause_window_draw(); - else + else resume_window_draw(); active = !active; } @@ -172,8 +174,8 @@ void monitor_procs(){ process_t* start_process_monitor(){ #if QEMU return create_kernel_process("procmonitor",monitor_procs); -#else +#else //TODO: disabled process monitor since shortcuts seem broken on rpi return 0x0;//create_kernel_process("procmonitor",monitor_procs); #endif -} \ No newline at end of file +} diff --git a/kernel/memory/mmu.c b/kernel/memory/mmu.c index 3b928c80..71313eb8 100644 --- a/kernel/memory/mmu.c +++ b/kernel/memory/mmu.c @@ -60,8 +60,8 @@ void mmu_map_2mb(uint64_t va, uint64_t pa, uint64_t attr_index) { l1[l1_index] = ((uint64_t)l2 & 0xFFFFFFFFF000ULL) | PD_TABLE; } - uint64_t* l2 = (uint64_t*)(l1[l1_index] & 0xFFFFFFFFF000ULL); - + uint64_t* l2 = (uint64_t*)(l1[l1_index] & 0xFFFFFFFFF000ULL); + //For now we make this not executable. We'll need to to separate read_write, read_only and executable sections uint64_t attr = ((uint64_t)1 << UXN_BIT) | ((uint64_t)0 << PXN_BIT) | (1 << AF_BIT) | (0b11 << SH_BIT) | (0b00 << AP_BIT) | (attr_index << MAIR_BIT) | PD_BLOCK; l2[l2_index] = (pa & 0xFFFFFFFFF000ULL) | attr; @@ -78,13 +78,13 @@ void mmu_map_4kb(uint64_t va, uint64_t pa, uint64_t attr_index, uint64_t level) uint64_t* l1 = (uint64_t*)talloc(PAGE_SIZE); page_table_l0[l0_index] = ((uint64_t)l1 & 0xFFFFFFFFF000ULL) | PD_TABLE; } - + uint64_t* l1 = (uint64_t*)(page_table_l0[l0_index] & 0xFFFFFFFFF000ULL); if (!(l1[l1_index] & 1)) { uint64_t* l2 = (uint64_t*)talloc(PAGE_SIZE); l1[l1_index] = ((uint64_t)l2 & 0xFFFFFFFFF000ULL) | PD_TABLE; } - + uint64_t* l2 = (uint64_t*)(l1[l1_index] & 0xFFFFFFFFF000ULL); uint64_t l2_val = l2[l2_index]; if (!(l2_val & 1)) { @@ -94,28 +94,28 @@ void mmu_map_4kb(uint64_t va, uint64_t pa, uint64_t attr_index, uint64_t level) kprintf("[MMU error]: Region not mapped for address %x, already mapped at higher granularity [%i][%i][%i][%i]",va, l0_index,l1_index,l2_index,l3_index); return; } - + uint64_t* l3 = (uint64_t*)(l2[l2_index] & 0xFFFFFFFFF000ULL); - + if (l3[l3_index] & 1){ kprintf("[MMU warning]: Section already mapped %x",va); return; } - - uint8_t permission; - + + uint8_t permission = 0; + switch (level) { case 0: permission = 0b01; break; case 1: permission = 0b00; break; case 2: permission = 0b10; break; - + default: break; } uint64_t attr = ((uint64_t)(level == 1) << UXN_BIT) | ((uint64_t)0 << PXN_BIT) | (1 << AF_BIT) | (0b01 << SH_BIT) | (permission << AP_BIT) | (attr_index << MAIR_BIT) | 0b11; kprintfv("[MMU] Mapping 4kb memory %x at [%i][%i][%i][%i] for EL%i = %x | %x permission: %i", va, l0_index,l1_index,l2_index,l3_index,level,pa,attr,permission); - + l3[l3_index] = (pa & 0xFFFFFFFFF000ULL) | attr; } @@ -136,18 +136,18 @@ static inline void mmu_flush_icache() { } void mmu_unmap(uint64_t va, uint64_t pa){ - + uint64_t l0_index = (va >> 39) & 0x1FF; uint64_t l1_index = (va >> 30) & 0x1FF; uint64_t l2_index = (va >> 21) & 0x1FF; uint64_t l3_index = (va >> 12) & 0x1FF; - + kprintfv("[MMU] Unmapping 4kb memory %x at [%i][%i][%i][%i] for EL1", va, l0_index,l1_index,l2_index, l3_index); if (!(page_table_l0[l0_index] & 1)) return; - + uint64_t* l1 = (uint64_t*)(page_table_l0[l0_index] & 0xFFFFFFFFF000ULL); if (!(l1[l1_index] & 1)) return; - + uint64_t* l2 = (uint64_t*)(l1[l1_index] & 0xFFFFFFFFF000ULL); uint64_t l3_val = l2[l2_index]; if (!(l3_val & 1)) return; @@ -155,7 +155,7 @@ void mmu_unmap(uint64_t va, uint64_t pa){ l2[l2_index] = 0; return; } - + uint64_t* l3 = (uint64_t*)(l2[l2_index] & 0xFFFFFFFFF000ULL); l3[l3_index] = 0; @@ -206,7 +206,7 @@ void mmu_init() { asm volatile ("isb"); asm volatile ("msr ttbr0_el1, %0" :: "r"(page_table_l0)); - + asm volatile ( "mrs x0, sctlr_el1\n" "orr x0, x0, #0x1\n" @@ -277,4 +277,4 @@ void debug_mmu_address(uint64_t va){ } kprintf("Entry: %x", l4_val); return; -} \ No newline at end of file +} diff --git a/kernel/networking/network.cpp b/kernel/networking/network.cpp index effa3743..63d63d6a 100644 --- a/kernel/networking/network.cpp +++ b/kernel/networking/network.cpp @@ -34,7 +34,7 @@ int net_rx_frame(sizedptr *out_frame) { } const net_l2l3_endpoint* network_get_local_endpoint() { - static net_l2l3_endpoint dummy = {0}; + static net_l2l3_endpoint dummy = {}; return dispatch ? &dispatch->get_local_ep() : &dummy; } @@ -57,4 +57,4 @@ driver_module net_module = (driver_module){ .write = 0, .seek = 0, .readdir = 0, -}; \ No newline at end of file +}; diff --git a/kernel/networking/network_dispatch.cpp b/kernel/networking/network_dispatch.cpp index 901b23e6..37badda0 100644 --- a/kernel/networking/network_dispatch.cpp +++ b/kernel/networking/network_dispatch.cpp @@ -37,7 +37,7 @@ bool NetworkDispatch::init() void NetworkDispatch::handle_download_interrupt() { if (!driver) return; - + sizedptr raw = driver->handle_receive_packet(recv_buffer); if (raw.size < sizeof(eth_hdr_t)) { return; @@ -127,21 +127,6 @@ bool NetworkDispatch::dequeue_packet_for(uint16_t pid, sizedptr *out) return true; } -static sizedptr make_user_copy(const sizedptr &src) -{ - sizedptr out{0, 0}; - uintptr_t mem = malloc(src.size); - if (!mem) return out; - - memcpy(reinterpret_cast(mem), - reinterpret_cast(src.ptr), - src.size); - - out.ptr = mem; - out.size = src.size; - return out; -} - sizedptr NetworkDispatch::make_copy(const sizedptr &in) { sizedptr out{0, 0}; diff --git a/kernel/networking/port_manager.c b/kernel/networking/port_manager.c index 51ededb7..f7337eda 100644 --- a/kernel/networking/port_manager.c +++ b/kernel/networking/port_manager.c @@ -5,9 +5,6 @@ static port_entry_t g_port_table[PROTO_COUNT][MAX_PORTS];//tab proto/port -static inline bool port_valid(uint16_t p) { - return p > 0 && p < MAX_PORTS; -} static inline bool proto_valid(protocol_t proto) { return (uint32_t)proto< PROTO_COUNT; } @@ -27,13 +24,13 @@ int port_alloc_ephemeral(protocol_t proto, port_recv_handler_t handler) { if (!proto_valid(proto)) return -1; - for (uint16_t p = PORT_MIN_EPHEMERAL; p <= PORT_MAX_EPHEMERAL; ++p) { + for (int p = PORT_MIN_EPHEMERAL; p <= PORT_MAX_EPHEMERAL; ++p) { port_entry_t *e = &g_port_table[proto][p]; if (!e->used) { e->used = true; e->pid = pid; e->handler = handler; - return (int)p; + return p; } } return -1; @@ -44,7 +41,7 @@ bool port_bind_manual(protocol_t proto, uint16_t pid, port_recv_handler_t handler) { - if (!proto_valid(proto) || !port_valid(port)) return false; + if (!proto_valid(proto)) return false; port_entry_t *e = &g_port_table[proto][port]; if (e->used) return false; e->used = true; @@ -57,7 +54,7 @@ bool port_unbind(protocol_t proto, uint16_t port, uint16_t pid) { - if (!proto_valid(proto) || !port_valid(port)) return false; + if (!proto_valid(proto)) return false; port_entry_t *e = &g_port_table[proto][port]; if (!e->used || e->pid != pid) return false; e->used = false; @@ -68,7 +65,7 @@ bool port_unbind(protocol_t proto, void port_unbind_all(uint16_t pid) { for (int pr = 0; pr < PROTO_COUNT; ++pr) { - for (uint16_t p = 1; p < MAX_PORTS; ++p) { + for (int p = 1; p < MAX_PORTS; ++p) { port_entry_t *e = &g_port_table[pr][p]; if (e->used && e->pid == pid) { e->used = false; @@ -80,17 +77,17 @@ void port_unbind_all(uint16_t pid) { } bool port_is_bound(protocol_t proto, uint16_t port) { - if (!proto_valid(proto) || !port_valid(port)) return false; + if (!proto_valid(proto)) return false; return g_port_table[proto][port].used; } uint16_t port_owner_of(protocol_t proto, uint16_t port) { - if (!proto_valid(proto) || !port_valid(port)) return PORT_FREE_OWNER; + if (!proto_valid(proto)) return PORT_FREE_OWNER; return g_port_table[proto][port].pid; } port_recv_handler_t port_get_handler(protocol_t proto, uint16_t port) { - if (!proto_valid(proto) || !port_valid(port)) return NULL; + if (!proto_valid(proto)) return NULL; return g_port_table[proto][port].used ? g_port_table[proto][port].handler : NULL; diff --git a/kernel/process/scheduler.c b/kernel/process/scheduler.c index 3c5b18d4..49dc5805 100644 --- a/kernel/process/scheduler.c +++ b/kernel/process/scheduler.c @@ -197,7 +197,7 @@ void sleep_process(uint64_t msec){ sleeping[sleep_count++] = (sleep_tracker){ .pid = processes[current_proc].id, .timestamp = timer_now_msec(), - .sleep_time = msec, + .sleep_time = msec, .valid = true }; } @@ -236,7 +236,7 @@ sizedptr list_processes(const char *path){ void *list_buffer = (char*)malloc(size); if (strlen(path, 100) == 0){ uint32_t count = 0; - + char *write_ptr = (char*)list_buffer + 4; process_t *processes = get_all_processes(); for (int i = 0; i < MAX_PROCS; i++){ @@ -266,11 +266,19 @@ FS_RESULT open_proc(const char *path, file *descriptor){ } size_t read_proc(file* fd, char *buf, size_t size, file_offset offset){ - + (void)fd; + (void)buf; + (void)size; + (void)offset; + return 0; } size_t write_proc(file* fd, const char *buf, size_t size, file_offset offset){ - + (void)fd; + (void)buf; + (void)size; + (void)offset; + return 0; } driver_module scheduler_module = (driver_module){ @@ -284,4 +292,4 @@ driver_module scheduler_module = (driver_module){ .write = write_proc, .seek = 0, .readdir = list_processes, -}; \ No newline at end of file +}; diff --git a/shared/Makefile b/shared/Makefile index 36a78307..cd1b61bc 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -1,11 +1,18 @@ #shared -CFLAGS := $(CFLAGS_BASE) -I. -I../kernel -Wno-unused-parameter + +include ../common.mk + +CPPFLAGS := -I. -I../kernel +CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) +CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) CLEAN_OBJS := $(shell find . -name '*.o') +CLEAN_DEPS := $(shell find . -name '*.d') C_SRC := $(shell find . -name '*.c') CPP_SRC := $(shell find . -name '*.cpp') ASM_SRC := $(shell find . -name '*.S') OBJ := $(C_SRC:.c=.o) $(ASM_SRC:.S=.o) $(CPP_SRC:.cpp=.o) +DEP := $(C_SRC:.c=.d) $(ASM_SRC:.S=.d) $(CPP_SRC:.cpp=.d) TARGET := libshared.a @@ -14,16 +21,18 @@ TARGET := libshared.a all: $(TARGET) $(TARGET): $(OBJ) - $(AR) rcs $@ $^ + $(VAR) rcs $@ $^ %.o: %.S - $(CC) $(CFLAGS) -c $< -o $@ + $(VAS) $(CFLAGS) -c $< -o $@ %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c $< -o $@ + $(VCC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ + $(VCXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(TARGET) + $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) + +-include $(DEP) diff --git a/shared/net/application_layer/csocket_http_client.cpp b/shared/net/application_layer/csocket_http_client.cpp index baac959b..38d27336 100644 --- a/shared/net/application_layer/csocket_http_client.cpp +++ b/shared/net/application_layer/csocket_http_client.cpp @@ -1,6 +1,5 @@ -#pragma once #include "csocket_http_client.h" -#include "socket_http_client.hpp" +#include "socket_http_client.hpp" #include "net/transport_layer/socket_tcp.hpp" extern "C" { @@ -14,6 +13,7 @@ extern "C" { http_client_handle_t http_client_create(uint16_t pid) { uintptr_t mem = malloc(sizeof(HTTPClient)); if (!mem) return NULL; + // TODO: check for correct fix here HTTPClient *cli = reinterpret_cast( (void*)mem ); return reinterpret_cast(new HTTPClient(pid)); } diff --git a/shared/net/application_layer/csocket_http_server.cpp b/shared/net/application_layer/csocket_http_server.cpp index 7398cf43..bdc7fb56 100644 --- a/shared/net/application_layer/csocket_http_server.cpp +++ b/shared/net/application_layer/csocket_http_server.cpp @@ -12,6 +12,7 @@ extern "C" { http_server_handle_t http_server_create(uint16_t pid) { void* raw = (void*)malloc(sizeof(HTTPServer)); if (!raw) return nullptr; + // TODO: check for correct fix here HTTPServer* srv = reinterpret_cast(raw); return reinterpret_cast(new HTTPServer(pid)); } diff --git a/shared/net/application_layer/dhcp.c b/shared/net/application_layer/dhcp.c index 431292df..4f6eaa20 100644 --- a/shared/net/application_layer/dhcp.c +++ b/shared/net/application_layer/dhcp.c @@ -5,8 +5,6 @@ #include "types.h" #include "net/transport_layer/csocket_udp.h" -static socket_handle_t g_dhcp_socket = NULL; - extern uintptr_t malloc(uint64_t size); extern void free(void *ptr, uint64_t size); extern void sleep(uint64_t ms); @@ -67,10 +65,10 @@ uint16_t dhcp_parse_option(const dhcp_packet *p, uint16_t wanted) { } i += len; } - return UINT16_MAX; + return UINT16_MAX; } uint8_t dhcp_option_len(const dhcp_packet *p, uint16_t idx) { - if (idx == 0 || idx + 1 >= sizeof(p->options)) return 0; + if (idx == 0 || idx + 1u >= sizeof(p->options)) return 0; return p->options[idx+1]; } diff --git a/shared/net/internet_layer/icmp.c b/shared/net/internet_layer/icmp.c index 11aa79d9..c87f1ab2 100644 --- a/shared/net/internet_layer/icmp.c +++ b/shared/net/internet_layer/icmp.c @@ -53,10 +53,7 @@ void create_icmp_packet(uintptr_t p, pkt->id = __builtin_bswap16(d->id); pkt->seq = __builtin_bswap16(d->seq); - if (d->payload) - memcpy(pkt->payload, d->payload, 56); - else - memset(pkt->payload, 0, 56); + memcpy(pkt->payload, d->payload, 56); pkt->checksum = 0; } diff --git a/shared/net/internet_layer/ipv4.c b/shared/net/internet_layer/ipv4.c index 6ee6545d..b46d06bb 100644 --- a/shared/net/internet_layer/ipv4.c +++ b/shared/net/internet_layer/ipv4.c @@ -106,7 +106,6 @@ void ip_input(uintptr_t ip_ptr, arp_table_put(sip, src_mac, 60000, false); uint32_t dip = __builtin_bswap32(hdr->dst_ip); - const net_cfg_t *cfg = ipv4_get_cfg(); //TODO manage special ip uintptr_t payload_ptr = ip_ptr + header_bytes; uint32_t payload_len = __builtin_bswap16(hdr->total_length) - header_bytes; diff --git a/shared/net/transport_layer/csocket_tcp.cpp b/shared/net/transport_layer/csocket_tcp.cpp index af408fb6..ceb86119 100644 --- a/shared/net/transport_layer/csocket_tcp.cpp +++ b/shared/net/transport_layer/csocket_tcp.cpp @@ -1,4 +1,3 @@ -#pragma once #include "net/transport_layer/socket_tcp.hpp" #include "net/transport_layer/socket.hpp" #include "csocket_tcp.h" diff --git a/shared/net/transport_layer/csocket_udp.cpp b/shared/net/transport_layer/csocket_udp.cpp index ea2ae548..4dae93c3 100644 --- a/shared/net/transport_layer/csocket_udp.cpp +++ b/shared/net/transport_layer/csocket_udp.cpp @@ -1,4 +1,3 @@ -#pragma once #include "net/transport_layer/socket_udp.hpp" #include "net/transport_layer/socket.hpp" #include "csocket_udp.h" diff --git a/shared/net/transport_layer/socket_tcp.hpp b/shared/net/transport_layer/socket_tcp.hpp index 59363d5c..ddf70238 100644 --- a/shared/net/transport_layer/socket_tcp.hpp +++ b/shared/net/transport_layer/socket_tcp.hpp @@ -11,7 +11,7 @@ #define KP(fmt, ...) \ do { kprintf(fmt, ##__VA_ARGS__); } while (0) - + extern "C" { void sleep(uint64_t ms); uintptr_t malloc(uint64_t size); @@ -154,7 +154,7 @@ class TCPSocket : public Socket { flow->payload = { (uintptr_t)buf, (uint32_t)len }; flow->flags = (1<state != TCP_STATE_CLOSED) { + if (f->local_port == local_port) { + if (f->state == TCP_LISTEN) { + if (remote_ip == 0 && remote_port == 0) { + return i; + } + } + if (f->remote.ip == remote_ip && f->remote.port == remote_port) { + return i; + } + } + } + } + return -1; +} + tcp_data* tcp_get_ctx(uint16_t local_port, uint32_t remote_ip, uint16_t remote_port) @@ -88,25 +108,6 @@ uint16_t tcp_compute_checksum(const void *segment, return htons((uint16_t)(~sum & 0xFFFF)); } -static int find_flow(uint16_t local_port, uint32_t remote_ip, uint16_t remote_port) { - for (int i = 0; i < MAX_TCP_FLOWS; ++i) { - tcp_flow_t *f = &tcp_flows[i]; - if (f->state != TCP_STATE_CLOSED) { - if (f->local_port == local_port) { - if (f->state == TCP_LISTEN) { - if (remote_ip == 0 && remote_port == 0) { - return i; - } - } - if (f->remote.ip == remote_ip && f->remote.port == remote_port) { - return i; - } - } - } - } - return -1; -} - static int allocate_flow_entry() { for (int i = 0; i < MAX_TCP_FLOWS; ++i) { if (tcp_flows[i].state == TCP_STATE_CLOSED) { @@ -295,7 +296,7 @@ tcp_result_t tcp_flow_send(tcp_data *flow_ctx) { if (!flow) { return TCP_INVALID; } - + uint8_t flags = flow_ctx->flags; uint8_t *payload_ptr = (uint8_t*) flow_ctx->payload.ptr; uint16_t payload_len = flow_ctx->payload.size; @@ -421,7 +422,6 @@ void tcp_input(uintptr_t ptr, uint32_t len, uint32_t src_ip, uint32_t dst_ip) { uint32_t seq = ntohl(hdr->sequence); uint32_t ack = ntohl(hdr->ack); uint8_t flags = hdr->flags; - uint16_t window = ntohs(hdr->window); int idx = find_flow(dst_port, src_ip, src_port); tcp_flow_t *flow = (idx >= 0 ? &tcp_flows[idx] : NULL); @@ -431,7 +431,6 @@ void tcp_input(uintptr_t ptr, uint32_t len, uint32_t src_ip, uint32_t dst_ip) { //TODO: use a syscall for the rng rng_t rng; rng_init_random(&rng); - tcp_flow_t *lf = &tcp_flows[listen_idx]; int new_idx = allocate_flow_entry(); if (new_idx < 0) return; @@ -441,7 +440,7 @@ void tcp_input(uintptr_t ptr, uint32_t len, uint32_t src_ip, uint32_t dst_ip) { flow->remote.port = src_port; flow->state = TCP_SYN_RECEIVED; flow->retries = TCP_SYN_RETRIES; - + uint32_t iss = rng_next32(&rng); flow->ctx.sequence = iss; flow->ctx.ack = seq + 1; @@ -544,6 +543,7 @@ void tcp_input(uintptr_t ptr, uint32_t len, uint32_t src_ip, uint32_t dst_ip) { ); return; } + // fall-through case TCP_FIN_WAIT_1: case TCP_FIN_WAIT_2: case TCP_CLOSE_WAIT: diff --git a/shared/net/transport_layer/tcp.h b/shared/net/transport_layer/tcp.h index 4a6f20c2..d017fc34 100644 --- a/shared/net/transport_layer/tcp.h +++ b/shared/net/transport_layer/tcp.h @@ -71,7 +71,7 @@ typedef enum { typedef struct { uint16_t local_port; net_l4_endpoint remote; - tcp_state_t state; + tcp_state_t state; tcp_data ctx; uint8_t retries; } tcp_flow_t; @@ -81,8 +81,6 @@ typedef struct { #define TCP_DATA_RETRIES 5 #define TCP_RETRY_TIMEOUT_MS 1000 -static int find_flow(uint16_t local_port, uint32_t remote_ip, uint16_t remote_port); - tcp_data* tcp_get_ctx(uint16_t local_port, uint32_t remote_ip, uint16_t remote_port); diff --git a/user/Makefile b/user/Makefile index 9aa864f1..5e5cabf1 100644 --- a/user/Makefile +++ b/user/Makefile @@ -1,11 +1,18 @@ #user -CFLAGS := $(CFLAGS_BASE) -I. -I../shared -Wno-unused-parameter -LDFLAGS := -T $(shell ls *.ld) + +include ../common.mk + +CPPFLAGS := -I. -I../shared +CFLAGS := $(CFLAGS_BASE) $(CPPFLAGS) +CXXFLAGS := $(CXXFLAGS_BASE) $(CPPFLAGS) +LDFLAGS := -T $(shell ls *.ld) CLEAN_OBJS := $(shell find . -name '*.o') +CLEAN_DEPS := $(shell find . -name '*.d') C_SRC := $(shell find . -name '*.c') CPP_SRC := $(shell find . -name '*.cpp') OBJ := $(C_SRC:.c=.o) $(CPP_SRC:.cpp=.o) +DEP := $(C_SRC:.c=.d) $(CPP_SRC:.cpp=.d) NAME := $(notdir $(CURDIR)) ELF := $(NAME).elf @@ -17,17 +24,19 @@ LOCATION := ../fs/redos/user/ all: $(LOCATION)$(TARGET) $(LOCATION)$(TARGET): $(OBJ) - $(LD) $(LDFLAGS) -o $(LOCATION)$(ELF) $(OBJ) ../shared/libshared.a + $(VLD) $(LDFLAGS) -o $(LOCATION)$(ELF) $(OBJ) ../shared/libshared.a $(OBJCOPY) -O binary $(LOCATION)$(ELF) $@ %.o: %.S - $(CC) $(CFLAGS) -c $< -o $@ + $(VAS) $(CFLAGS) -c $< -o $@ %.o: %.c - $(CC) $(CFLAGS) $(CONLY_FLAGS_BASE) -c $< -o $@ + $(VCC) $(CFLAGS) -c -MMD -MP $< -o $@ %.o: %.cpp - $(CC) $(CFLAGS) -fno-rtti -c $< -o $@ + $(VCXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ clean: - rm -f $(CLEAN_OBJS) $(TARGET) + $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) + +-include $(DEP)