repo_name
stringlengths
6
97
path
stringlengths
3
341
text
stringlengths
8
1.02M
willmexe/opuntiaOS
kernel/include/mem/boot.h
<reponame>willmexe/opuntiaOS<filename>kernel/include/mem/boot.h #ifndef _KERNEL_MEM_BOOT_H #define _KERNEL_MEM_BOOT_H #include <libkern/types.h> struct memory_map { uint32_t startLo; uint32_t startHi; uint32_t sizeLo; uint32_t sizeHi; uint32_t type; uint32_t acpi_3_0; }; typedef struct memory_map memory_map_t; struct boot_desc { size_t paddr; size_t vaddr; void* memory_map; size_t memory_map_size; size_t kernel_size; void* devtree; }; typedef struct boot_desc boot_desc_t; #endif // _KERNEL_MEM_BOOT_H
willmexe/opuntiaOS
libs/libc/posix/sched.c
<reponame>willmexe/opuntiaOS #include <sched.h> #include <sysdep.h> #include <unistd.h> void sched_yield() { DO_SYSCALL_0(SYS_SCHED_YIELD); } int nice(int inc) { int res = DO_SYSCALL_1(SYS_NICE, inc); RETURN_WITH_ERRNO(res, 0, -1); }
willmexe/opuntiaOS
boot/aarch32/vmm/vmm.c
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include "vmm.h" #include <libboot/log/log.h> #include <libboot/mem/malloc.h> #include <libboot/mem/mem.h> #include <libboot/types.h> // #define DEBUG_VMM #define VMM_OFFSET_IN_DIRECTORY(a) (((a) >> 20) & 0xfff) #define VMM_OFFSET_IN_TABLE(a) (((a) >> 12) & 0xff) #define VMM_OFFSET_IN_PAGE(a) ((a)&0xfff) #define ALIGN_TO_TABLE(a) ((a)&0xfff00000) static pdirectory_t* pdir = NULL; static ptable_t* map_table(); static void mmu_enable(); static inline void write_ttbcr(uint32_t val) { asm volatile("mcr p15, 0, %0, c2, c0, 2" : : "r"(val) : "memory"); asm volatile("dmb"); } static inline void write_ttbr0(uint32_t val) { asm volatile("mcr p15, 0, %0, c2, c0, 0" : : "r"(val) : "memory"); asm volatile("dmb"); } static inline void write_dacr(uint32_t val) { asm volatile("mcr p15, 0, %0, c3, c0, 0" : : "r"(val)); asm volatile("dmb"); } static void mmu_enable() { volatile uint32_t val; asm volatile("mrc p15, 0, %0, c1, c0, 0" : "=r"(val)); asm volatile("orr %0, %1, #0x1" : "=r"(val) : "r"(val)); asm volatile("mcr p15, 0, %0, c1, c0, 0" ::"r"(val) : "memory"); asm volatile("isb"); } static pdirectory_t* vm_alloc_pdir() { return (pdirectory_t*)malloc_aligned(sizeof(pdirectory_t), sizeof(pdirectory_t)); } static ptable_t* vm_alloc_ptable() { return (ptable_t*)malloc_aligned(sizeof(ptable_t), sizeof(ptable_t)); } static ptable_t* map_table(size_t tphyz, size_t tvirt) { ptable_t* table = vm_alloc_ptable(); for (size_t phyz = tphyz, virt = tvirt, i = 0; i < VMM_PTE_COUNT; phyz += VMM_PAGE_SIZE, virt += VMM_PAGE_SIZE, i++) { page_desc_t new_page; new_page.one = 1; new_page.baddr = (phyz / VMM_PAGE_SIZE); new_page.tex = 0b001; new_page.c = 1; new_page.b = 1; new_page.ap1 = 0b11; new_page.ap2 = 0b0; new_page.s = 1; table->entities[i] = new_page; } uint32_t table_int = (uint32_t)table; pdir->entities[VMM_OFFSET_IN_DIRECTORY(tvirt)].valid = 1; pdir->entities[VMM_OFFSET_IN_DIRECTORY(tvirt)].zero1 = 0; pdir->entities[VMM_OFFSET_IN_DIRECTORY(tvirt)].zero2 = 0; pdir->entities[VMM_OFFSET_IN_DIRECTORY(tvirt)].ns = 0; pdir->entities[VMM_OFFSET_IN_DIRECTORY(tvirt)].zero3 = 0; pdir->entities[VMM_OFFSET_IN_DIRECTORY(tvirt)].domain = 0b0011; pdir->entities[VMM_OFFSET_IN_DIRECTORY(tvirt)].baddr = ((table_int / 1024)); return table; } static void vm_init() { pdir = vm_alloc_pdir(); for (int i = 0; i < VMM_PDE_COUNT; i++) { pdir->entities[i].valid = 0; } } static void vm_map_devices() { map_table(0x1c000000, 0x1c000000); // mapping uart } void vm_setup(size_t kernel_vaddr, size_t kernel_paddr, size_t kernel_size) { vm_init(); vm_map_devices(); extern int bootloader_start[]; size_t bootloader_start_aligned = ALIGN_TO_TABLE((size_t)bootloader_start); map_table(bootloader_start_aligned, bootloader_start_aligned); #ifdef DEBUG_VMM log("map %x to %x", bootloader_start_aligned, bootloader_start_aligned); #endif size_t table_paddr = ALIGN_TO_TABLE(kernel_paddr); size_t table_vaddr = ALIGN_TO_TABLE(kernel_vaddr); const size_t bytes_per_table = VMM_PTE_COUNT * VMM_PAGE_SIZE; const size_t tables_per_kernel = align_size((kernel_size + bytes_per_table - 1) / bytes_per_table, 4); for (int i = 0; i < tables_per_kernel; i++) { map_table(table_paddr, table_paddr); map_table(table_paddr, table_vaddr); #ifdef DEBUG_VMM log("map %x to %x", table_paddr, table_paddr); log("map %x to %x", table_paddr, table_vaddr); #endif table_paddr += bytes_per_table; table_vaddr += bytes_per_table; } write_ttbr0((size_t)(pdir)); write_dacr(0x55555555); mmu_enable(); } void vm_setup_secondary_cpu() { write_ttbr0((uint32_t)(pdir)); write_dacr(0x55555555); mmu_enable(); }
willmexe/opuntiaOS
kernel/include/algo/ringbuffer.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_ALGO_RINGBUFFER_H #define _KERNEL_ALGO_RINGBUFFER_H #include <libkern/libkern.h> #include <libkern/lock.h> #include <mem/kmemzone.h> #define RINGBUFFER_STD_SIZE (16 * KB) struct __ringbuffer { kmemzone_t zone; size_t start; size_t end; }; typedef struct __ringbuffer ringbuffer_t; ringbuffer_t ringbuffer_create(size_t size); #define ringbuffer_create_std() ringbuffer_create(RINGBUFFER_STD_SIZE) void ringbuffer_free(ringbuffer_t* buf); size_t ringbuffer_space_to_read(ringbuffer_t* buf); size_t ringbuffer_space_to_read_with_custom_start(ringbuffer_t* buf, size_t start); size_t ringbuffer_space_to_write(ringbuffer_t* buf); size_t ringbuffer_read(ringbuffer_t* buf, uint8_t*, size_t); size_t ringbuffer_read_with_start(ringbuffer_t* buf, size_t start, uint8_t* holder, size_t siz); size_t ringbuffer_write(ringbuffer_t* buf, const uint8_t*, size_t); size_t ringbuffer_write_ignore_bounds(ringbuffer_t* buf, const uint8_t* holder, size_t siz); size_t ringbuffer_read_one(ringbuffer_t* buf, uint8_t* data); size_t ringbuffer_write_one(ringbuffer_t* buf, uint8_t data); void ringbuffer_clear(ringbuffer_t* buf); #endif //_KERNEL_ALGO_RINGBUFFER_H
willmexe/opuntiaOS
kernel/kernel/mem/pmm.c
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <algo/bitmap.h> #include <libkern/libkern.h> #include <libkern/log.h> #include <mem/pmm.h> // define DEBUG_PMM static void _pmm_init_ram(); static void _pmm_allocate_mat(); static pmm_state_t pmm_state; static inline void* _pmm_block_id_to_ptr(size_t value) { return (void*)((value * PMM_BLOCK_SIZE) + pmm_state.ram_offset); } static inline size_t _pmm_ptr_to_block_id(void* value) { return ((uintptr_t)value - pmm_state.ram_offset) / PMM_BLOCK_SIZE; } void _pmm_mark_avail_region(size_t region_start, size_t region_len) { region_start = ROUND_CEIL(region_start, PMM_BLOCK_SIZE) - pmm_state.ram_offset; region_len = ROUND_FLOOR(region_len, PMM_BLOCK_SIZE); #ifdef DEBUG_PMM log("PMM: Marked as avail: %x - %x", region_start + pmm_state.ram_offset, region_len); #endif size_t block_id = region_start / PMM_BLOCK_SIZE; size_t blocks_len = region_len / PMM_BLOCK_SIZE; pmm_state.used_blocks -= blocks_len; bitmap_unset_range(pmm_state.mat, block_id, blocks_len); } void _pmm_mark_used_region(size_t region_start, size_t region_len) { region_start = ROUND_FLOOR(region_start, PMM_BLOCK_SIZE) - pmm_state.ram_offset; region_len = ROUND_CEIL(region_len, PMM_BLOCK_SIZE); #ifdef DEBUG_PMM log("PMM: Marked as used: %x - %x", region_start + pmm_state.ram_offset, region_len); #endif size_t block_id = region_start / PMM_BLOCK_SIZE; size_t blocks_len = region_len / PMM_BLOCK_SIZE; pmm_state.used_blocks -= blocks_len; bitmap_set_range(pmm_state.mat, block_id, blocks_len); } static void _pmm_init_ram() { pmm_state.ram_offset = (size_t)-1; pmm_state.ram_size = 0; memory_map_t* memory_map = (memory_map_t*)pmm_state.boot_desc->memory_map; for (int i = 0; i < pmm_state.boot_desc->memory_map_size; i++) { if (memory_map[i].type == 1) { pmm_state.ram_offset = min(pmm_state.ram_offset, memory_map[i].startLo); pmm_state.ram_size = max(pmm_state.ram_size, memory_map[i].startLo + memory_map[i].sizeLo); } } } static void _pmm_allocate_mat() { size_t mat_cover_len = pmm_state.ram_size - pmm_state.ram_offset; pmm_state.mat = bitmap_wrap((void*)(pmm_state.kernel_va_base + pmm_state.kernel_size), mat_cover_len / PMM_BLOCK_SIZE); pmm_state.max_blocks = mat_cover_len / PMM_BLOCK_SIZE; pmm_state.used_blocks = pmm_state.max_blocks; memset(pmm_state.mat.data, 0xff, pmm_state.mat.len / 8); } static void _pmm_init_mat() { memory_map_t* memory_map = (memory_map_t*)pmm_state.boot_desc->memory_map; for (int i = 0; i < pmm_state.boot_desc->memory_map_size; i++) { if (memory_map[i].type == 1) { #ifdef DEBUG_PMM log(" %d: %x - %x", i, memory_map[i].startLo, memory_map[i].sizeLo); #endif _pmm_mark_avail_region(memory_map[i].startLo, memory_map[i].sizeLo); } } } static void _pmm_init_from_desc(boot_desc_t* boot_desc) { pmm_state.boot_desc = boot_desc; pmm_state.kernel_va_base = ROUND_CEIL(boot_desc->vaddr, PMM_BLOCK_SIZE); pmm_state.kernel_size = ROUND_CEIL(boot_desc->kernel_size * 1024, PMM_BLOCK_SIZE); _pmm_init_ram(); _pmm_allocate_mat(); _pmm_init_mat(); // Marking all locations before kernel as used. if (boot_desc->paddr > pmm_state.ram_offset) { _pmm_mark_used_region(pmm_state.ram_offset, boot_desc->paddr - pmm_state.ram_offset); } // Marking kernel as used. _pmm_mark_used_region(boot_desc->paddr, boot_desc->kernel_size * 1024); // Marking MAT as used. size_t mat_pa_base = (size_t)pmm_state.mat.data - boot_desc->vaddr + boot_desc->paddr; _pmm_mark_used_region(mat_pa_base, pmm_state.mat.len / 8); } void pmm_setup(boot_desc_t* boot_desc) { _pmm_init_from_desc(boot_desc); } static void* pmm_alloc_blocks(size_t count) { int block_id = bitmap_find_space(pmm_state.mat, count); if (block_id < 0) { return NULL; } bitmap_set_range(pmm_state.mat, block_id, count); pmm_state.used_blocks += count; return _pmm_block_id_to_ptr(block_id); } static void* pmm_alloc_blocks_aligned(size_t count, size_t align) { int block_id = bitmap_find_space_aligned(pmm_state.mat, count, align); if (block_id < 0) { return NULL; } bitmap_set_range(pmm_state.mat, block_id, count); pmm_state.used_blocks += count; return _pmm_block_id_to_ptr(block_id); } static int pmm_free_blocks(size_t block_id, size_t count) { pmm_state.used_blocks -= count; return bitmap_unset_range(pmm_state.mat, block_id, count); } void* pmm_alloc(size_t size) { size_t block_count = (size + PMM_BLOCK_SIZE - 1) / PMM_BLOCK_SIZE; return pmm_alloc_blocks(block_count); } void* pmm_alloc_aligned(size_t size, size_t align) { size_t block_count = (size + PMM_BLOCK_SIZE - 1) / PMM_BLOCK_SIZE; size_t block_align = (align + PMM_BLOCK_SIZE - 1) / PMM_BLOCK_SIZE; if (block_align == 1) { return pmm_alloc_blocks(block_count); } return pmm_alloc_blocks_aligned(block_count, block_align); } int pmm_free(void* block, size_t size) { size_t block_id = _pmm_ptr_to_block_id(block); size_t block_count = (size + PMM_BLOCK_SIZE - 1) / PMM_BLOCK_SIZE; return pmm_free_blocks(block_id, block_count); } size_t pmm_get_ram_size() { return pmm_state.ram_size; } size_t pmm_get_max_blocks() { return pmm_state.max_blocks; } size_t pmm_get_used_blocks() { return pmm_state.used_blocks; } size_t pmm_get_free_blocks() { return pmm_get_max_blocks() - pmm_get_used_blocks(); } size_t pmm_get_block_size() { return PMM_BLOCK_SIZE; } size_t pmm_get_ram_in_kb() { return pmm_get_ram_size() / 1024; } size_t pmm_get_free_space_in_kb() { return pmm_get_free_blocks() * (PMM_BLOCK_SIZE / 1024); } const pmm_state_t* pmm_get_state() { return &pmm_state; }
willmexe/opuntiaOS
kernel/kernel/platform/aarch32/tasking/dump_impl.c
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <libkern/log.h> #include <mem/vmm.h> #include <platform/generic/system.h> #include <platform/x86/tasking/dump_impl.h> void dump_regs(dump_data_t* dump_data) { } void dump_backtrace(dump_data_t* dump_data) { } int dump_impl(dump_data_t* dump_data) { char buf[64]; trapframe_t* tf = dump_data->p->main_thread->tf; snprintf(buf, 64, "Dump not supported for arm target yet!\n"); dump_data->writer(buf); return 0; } int dump_kernel_impl(dump_data_t* dump_data, const char* err_desc) { return 0; } int dump_kernel_impl_from_tf(dump_data_t* dump_data, const char* err_desc, trapframe_t* tf) { return 0; }
willmexe/opuntiaOS
libs/libui/include/libui/ViewController.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include <libfoundation/Event.h> #include <libfoundation/EventLoop.h> #include <libfoundation/EventReceiver.h> #include <libui/View.h> #include <memory> #include <sys/types.h> namespace UI { class BaseViewController : public LFoundation::EventReceiver { public: BaseViewController() = default; virtual ~BaseViewController() = default; virtual void view_did_load() { } virtual void receive_event(std::unique_ptr<LFoundation::Event> event) override { if (event->type() == Event::Type::ViewDidLoad) { view_did_load(); } } private: }; template <class ViewT> class ViewController : public BaseViewController { public: ViewController(ViewT& view) : m_view(view) { } virtual ~ViewController() = default; ViewT& view() { return m_view; } const ViewT& view() const { return m_view; } Window& window() { return *m_view.window(); } private: ViewT& m_view; }; } // namespace UI
willmexe/opuntiaOS
libs/libc/sysdeps/unix/generic/ioctl.c
#include <sys/ioctl.h> #include <sysdep.h> int ioctl(int fd, uint32_t cmd, uint32_t arg) { int res = DO_SYSCALL_3(SYS_IOCTL, fd, cmd, arg); RETURN_WITH_ERRNO(res, res, -1); }
willmexe/opuntiaOS
kernel/include/mem/bits/zone.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_MEM_BITS_ZONE_H #define _KERNEL_MEM_BITS_ZONE_H #include <mem/bits/mmu.h> enum ZONE_FLAGS { ZONE_WRITABLE = MMU_FLAG_PERM_WRITE, ZONE_READABLE = MMU_FLAG_PERM_READ, ZONE_EXECUTABLE = MMU_FLAG_PERM_EXEC, ZONE_NOT_CACHEABLE = MMU_FLAG_UNCACHED, ZONE_COW = MMU_FLAG_COW, ZONE_USER = MMU_FLAG_NONPRIV, }; enum ZONE_TYPES { ZONE_TYPE_NULL = 0x0, ZONE_TYPE_CODE = 0x1, ZONE_TYPE_DATA = 0x2, ZONE_TYPE_STACK = 0x4, ZONE_TYPE_BSS = 0x8, ZONE_TYPE_DEVICE = 0x10, ZONE_TYPE_MAPPED = 0x20, ZONE_TYPE_MAPPED_FILE_PRIVATLY = 0x40, ZONE_TYPE_MAPPED_FILE_SHAREDLY = 0x80, }; #endif // _KERNEL_MEM_BITS_ZONE_H
willmexe/opuntiaOS
libs/libg/include/libg/ImageLoaders/PNGLoader.h
<gh_stars>100-1000 /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include <libfoundation/ByteOrder.h> #include <libg/Color.h> #include <libg/PixelBitmap.h> #include <libg/Rect.h> #include <string> namespace LG { namespace PNG { struct ChunkHeader { size_t len; uint8_t type[4]; }; struct IHDRChunk { uint32_t width; uint32_t height; uint8_t depth; uint8_t color_type; uint8_t compression_method; uint8_t filter_method; uint8_t interlace_method; }; class DataStreamer { public: DataStreamer() = default; DataStreamer(const void* data) : m_ptr((uint8_t*)data) { } ~DataStreamer() = default; template <typename T> void read(T& val) { val = *(T*)m_ptr; m_ptr += sizeof(T); val = LFoundation::ByteOrder::from_network(val); } void read(void* buffer, size_t cnt) { memcpy((uint8_t*)buffer, m_ptr, cnt); m_ptr += cnt; } template <typename T> T at(int index) const { T val = *((T*)m_orig_ptr + index); val = LFoundation::ByteOrder::from_network(val); return val; } void skip(size_t cnt) { m_ptr += cnt; } void set(const void* data) { m_orig_ptr = m_ptr = (uint8_t*)data; } const uint8_t* ptr() const { return m_ptr; } uint8_t* ptr() { return m_ptr; } private: uint8_t* m_orig_ptr { nullptr }; uint8_t* m_ptr { nullptr }; }; class Scanline { public: Scanline() = default; Scanline(int type, void* ptr) : m_filter_type(type) , m_ptr(ptr) { } ~Scanline() = default; void set(int type, void* ptr) { m_ptr = ptr; } int filter_type() const { return m_filter_type; } uint8_t* data() const { return (uint8_t*)m_ptr; } private: int m_filter_type { 0 }; void* m_ptr { nullptr }; size_t m_len { 0 }; }; class ScanlineKeeper { public: ScanlineKeeper() = default; ~ScanlineKeeper() { invalidate(); } inline void init(void* ptr) { m_ptr = ptr; } inline void init(void* ptr, uint8_t color_length) { m_ptr = ptr, m_color_length = color_length; } void invalidate() { if (m_ptr) { free(m_ptr); m_data.clear(); m_ptr = nullptr; } } inline void add(Scanline&& el) { m_data.push_back(std::move(el)); } inline void set_color_length(uint8_t color_length) { m_color_length = color_length; } inline uint8_t color_length() const { return m_color_length; } inline std::vector<Scanline>& scanlines() { return m_data; } inline const std::vector<Scanline>& scanlines() const { return m_data; } private: uint8_t m_color_length { 0 }; void* m_ptr { nullptr }; std::vector<Scanline> m_data; }; class PNGLoader { public: PNGLoader() = default; ~PNGLoader() = default; PixelBitmap load_from_file(const std::string& path); PixelBitmap load_from_mem(const uint8_t* ptr); inline DataStreamer& streamer() { return m_streamer; } private: bool check_header(const uint8_t* ptr) const; void proccess_stream(PixelBitmap& bitmap); void process_compressed_data(PixelBitmap& bitmap); bool read_chunk(PixelBitmap& bitmap); void read_IHDR(ChunkHeader& header, PixelBitmap& bitmap); void read_TEXT(ChunkHeader& header, PixelBitmap& bitmap); void read_PHYS(ChunkHeader& header, PixelBitmap& bitmap); void read_ORNT(ChunkHeader& header, PixelBitmap& bitmap); void read_IDAT(ChunkHeader& header, PixelBitmap& bitmap); uint8_t paeth_predictor(int a, int b, int c); void unfilter_scanlines(); void copy_scanlines_to_bitmap(PixelBitmap& bitmap); std::vector<uint8_t> m_compressed_data; DataStreamer m_streamer; IHDRChunk m_ihdr_chunk; ScanlineKeeper m_scanline_keeper; }; } // namespace PNG } // namespace LG
willmexe/opuntiaOS
libs/libobjc/include/libobjc/v1/decls.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _LIBOBJC_V1_DECLS_H #define _LIBOBJC_V1_DECLS_H #include <stdint.h> struct objc_class; struct objc_object; struct objc_selector; struct objc_method; typedef struct objc_class* Class; typedef struct objc_object* id; typedef struct objc_selector* SEL; typedef struct objc_method* Method; typedef void (*IMP)(id, SEL, ...); #define nil_method (IMP) NULL #define nil (id) NULL #define Nil (Class) NULL struct objc_symtab { unsigned long sel_ref_cnt; struct objc_selector* refs; unsigned short cls_def_cnt; unsigned short cat_def_cnt; void* defs[1]; // Variable len }; struct objc_module { unsigned long version; unsigned long size; const char* name; struct objc_symtab* symtab; }; struct objc_object final { Class isa; inline void set_isa(Class nisa) { isa = nisa; } inline Class get_isa() { return isa; } }; struct objc_ivar { const char* ivar_name; const char* ivar_type; int ivar_offset; }; struct objc_ivar_list { int ivar_count; struct objc_ivar ivar_list[1]; // Variable len }; struct objc_method { SEL method_name; const char* method_types; IMP method_imp; }; struct objc_method_description { SEL name; char* types; }; struct objc_method_list { struct objc_method_list* method_next; int method_count; struct objc_method method_list[1]; // Variable len }; struct objc_method_description_list { int count; struct objc_method_description list[1]; // Variable len }; struct objc_protocol { struct objc_class* class_pointer; char* protocol_name; struct objc_protocol_list* protocol_list; struct objc_method_description_list* instance_methods; struct objc_method_description_list* class_methods; }; struct objc_protocol_list { struct objc_protocol_list* next; uint32_t count; struct objc_protocol* list[1]; // Variable len }; #define CLS_CLASS 0x1 #define CLS_META 0x2 #define CLS_INITIALIZED 0x4 #define CLS_RESOLVED 0x8 // This means that it has had correct super and sublinks assigned #define CLS_INCONSTRUCTION 0x10 #define CLS_NUMBER_OFFSET 16 // Long is 32bit long on our target, we use 16bit for number struct objc_class final { Class isa; Class superclass; const char* name; long version; unsigned long info; long instance_size; struct objc_ivar_list* ivars; struct objc_method_list* methods; void* disp_table; struct objc_class* subclass_list; // TODO: Currently unresolved struct objc_class* sibling_class; // TODO: Currently unresolved struct objc_protocol_list* protocols; void* gc_object_type; inline void set_isa(Class nisa) { isa = nisa; } inline Class get_isa() { return isa; } inline void set_info(unsigned long mask) { info = mask; } inline void add_info(unsigned long mask) { info |= mask; } inline void rem_info(unsigned long mask) { info &= (~mask); } inline bool has_info(unsigned long mask) { return ((info & mask) == mask); } inline unsigned long get_info() { return info; } inline bool is_class() { return has_info(CLS_CLASS); } inline bool is_meta() { return has_info(CLS_META); } inline bool is_resolved() { return has_info(CLS_RESOLVED); } inline bool is_initialized() { return has_info(CLS_INITIALIZED); } inline bool is_root() { return (bool)superclass; } inline void set_number(int num) { info = (info & ((1 << CLS_NUMBER_OFFSET) - 1)) | (num << CLS_NUMBER_OFFSET); } inline int number() { return get_info() >> CLS_NUMBER_OFFSET; } inline long size() { return instance_size; } inline long aligned_size() { return instance_size; } // FIXME }; struct objc_selector { void* id; const char* types; }; #endif // _LIBOBJC_V1_DECLS_H
willmexe/opuntiaOS
libs/libipc/include/libipc/ServerConnection.h
<gh_stars>100-1000 #pragma once #include <cstdlib> #include <libfoundation/Logger.h> #include <libipc/Message.h> #include <libipc/MessageDecoder.h> #include <vector> template <typename ServerDecoder, typename ClientDecoder> class ServerConnection { public: ServerConnection(int sock_fd, ServerDecoder& server_decoder, ClientDecoder& client_decoder) : m_connection_fd(sock_fd) , m_server_decoder(server_decoder) , m_client_decoder(client_decoder) { } bool send_message(const Message& msg) const { auto encoded_msg = msg.encode(); int wrote = write(m_connection_fd, encoded_msg.data(), encoded_msg.size()); return wrote == encoded_msg.size(); } void pump_messages() { std::vector<char> buf; char tmpbuf[1024]; int read_cnt; while ((read_cnt = read(m_connection_fd, tmpbuf, sizeof(tmpbuf)))) { if (read_cnt <= 0) { Logger::debug << getpid() << " :: ServerConnection read error" << std::endl; return; } size_t buf_size = buf.size(); buf.resize(buf_size + read_cnt); memcpy(&buf.data()[buf_size], tmpbuf, read_cnt); if (read_cnt < sizeof(tmpbuf)) { break; } } size_t msg_len = 0; size_t buf_size = buf.size(); for (int i = 0; i < buf_size; i += msg_len) { msg_len = 0; if (auto response = m_server_decoder.decode((buf.data() + i), read_cnt - i, msg_len)) { if (auto answer = m_server_decoder.handle(*response)) { send_message(*answer); } } else if (auto response = m_client_decoder.decode((buf.data() + i), read_cnt - i, msg_len)) { } else { std::abort(); } } } private: int m_connection_fd; ServerDecoder& m_server_decoder; ClientDecoder& m_client_decoder; };
willmexe/opuntiaOS
userland/system/dock/WindowEntity.h
<gh_stars>100-1000 #pragma once #include <libg/PixelBitmap.h> #include <string> class WindowEntity { public: enum Status : uint32_t { Minimized = (1 << 0), }; WindowEntity() : m_window_id(0) , m_title() { } WindowEntity(int window_id) : m_window_id(window_id) , m_title() { } bool operator==(const WindowEntity& other) const { return m_window_id == other.m_window_id; } bool operator!=(const WindowEntity& other) const { return m_window_id != other.m_window_id; } int window_id() const { return m_window_id; } bool is_minimized() const { return has_attr(Status::Minimized); } void set_minimized(bool b) { b ? set_attr(Status::Minimized) : rem_attr(Status::Minimized); } const std::string& title() const { return m_title; } void set_title(const std::string& title) { m_title = title; } void set_title(std::string&& title) { m_title = std::move(title); } private: inline bool has_attr(Status mode) const { return ((m_window_status & (uint32_t)mode) == (uint32_t)mode); } inline void set_attr(Status mode) { m_window_status |= (uint32_t)mode; } inline void rem_attr(Status mode) { m_window_status = m_window_status & (~(uint32_t)mode); } int m_window_id { 0 }; uint32_t m_window_status { 0 }; std::string m_title {}; };
willmexe/opuntiaOS
libs/libui/include/libui/Connection.h
<reponame>willmexe/opuntiaOS #pragma once #include <libipc/ClientConnection.h> #include <libui/ClientDecoder.h> #include <sys/types.h> namespace UI { class Window; class Connection { public: static Connection& the(); explicit Connection(int connection_fd); void greeting(); int new_window(const Window& window); void set_buffer(const Window& window); template <class T> inline std::unique_ptr<T> send_sync_message(const Message& msg) { return std::unique_ptr<T>(m_connection_with_server.send_sync(msg)); } inline bool send_async_message(const Message& msg) const { return m_connection_with_server.send_message(msg); } inline void listen() { m_connection_with_server.pump_messages(); } // We use connection id as an unique key. inline int key() const { return m_connection_id; } private: void setup_listners(); int m_connection_fd; int m_connection_id; ClientConnection<BaseWindowServerDecoder, ClientDecoder> m_connection_with_server; BaseWindowServerDecoder m_server_decoder; ClientDecoder m_client_decoder; }; } // namespace UI
willmexe/opuntiaOS
libs/libc/include/dirent.h
#ifndef _LIBC_DIRENT_H #define _LIBC_DIRENT_H #include <stddef.h> #include <sys/_structs.h> #include <sys/cdefs.h> #include <sys/types.h> __BEGIN_DECLS struct __dirstream { int fd; size_t size; /* size of dir data */ size_t allocated; /* size of data holder block */ size_t offset; }; typedef struct __dirstream DIR; struct __dirent { uint32_t d_ino; /* Inode number */ uint32_t d_off; /* Offset to next linux_dirent */ uint16_t d_reclen; /* Length of this linux_dirent */ char d_name[]; /* Filename (null-terminated) */ }; typedef struct __dirent dirent_t; ssize_t getdents(int fd, char* buf, size_t len); __END_DECLS #endif /* _LIBC_DIRENT_H */
willmexe/opuntiaOS
libs/libcxxabi/include/cxxabi.h
<reponame>willmexe/opuntiaOS<filename>libs/libcxxabi/include/cxxabi.h #ifndef _CXXABI_H #define _CXXABI_H #define VIS_HIDDEN __attribute__((visibility("hidden"))) #define VIS_INTERNAL __attribute__((visibility("internal"))) extern "C" { int __cxa_atexit(void (*)(void*), void*, void*); void __cxa_finalize(void*); } #endif /* _CXXABI_H */
willmexe/opuntiaOS
boot/libboot/abi/memory.h
<gh_stars>100-1000 /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _BOOT_LIBBOOT_ABI_MEMORY_H #define _BOOT_LIBBOOT_ABI_MEMORY_H #include <libboot/types.h> struct memory_map { uint32_t startLo; uint32_t startHi; uint32_t sizeLo; uint32_t sizeHi; uint32_t type; uint32_t acpi_3_0; }; typedef struct memory_map memory_map_t; struct mem_desc { uint16_t memory_map_size; uint16_t kernel_size; }; typedef struct mem_desc mem_desc_t; struct boot_desc { size_t paddr; size_t vaddr; void* memory_map; size_t memory_map_size; size_t kernel_size; void* devtree; }; typedef struct boot_desc boot_desc_t; #endif // _BOOT_LIBBOOT_ABI_MEMORY_H
willmexe/opuntiaOS
libs/libc/include/sys/ioctl.h
#ifndef _LIBC_SYS_IOCTL_H #define _LIBC_SYS_IOCTL_H #include <bits/sys/ioctls.h> #include <stddef.h> #include <sys/cdefs.h> #include <sys/types.h> __BEGIN_DECLS int ioctl(int fd, uint32_t cmd, uint32_t arg); __END_DECLS #endif // _LIBC_SYS_IOCTL_H
willmexe/opuntiaOS
libs/libfoundation/include/libfoundation/Memory.h
<reponame>willmexe/opuntiaOS /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include <cstddef> namespace LFoundation { [[gnu::always_inline]] inline void fast_copy(uint32_t* dest, const uint32_t* src, std::size_t count) { #ifdef __i386__ asm volatile( "rep movsl\n" : "=S"(src), "=D"(dest), "=c"(count) : "S"(src), "D"(dest), "c"(count) : "memory"); #elif __arm__ while (count--) { asm("pld [%0, #128]" ::"r"(src)); *dest++ = *src++; } #endif } [[gnu::always_inline]] inline void fast_set(uint32_t* dest, uint32_t val, std::size_t count) { #ifdef __i386__ asm volatile( "rep stosl\n" : "=D"(dest), "=c"(count) : "D"(dest), "c"(count), "a"(val) : "memory"); #elif __arm__ asm volatile( "cmp %[count], #0\n" "beq fast_set_exit%=\n" "tst %[ptr], #15\n" "beq fast_set_16bytes_aligned_entry%=\n" "fast_set_4bytes_aligned_loop%=:\n" "subs %[count], %[count], #1\n" "str %[value], [%[ptr]], #4\n" "beq fast_set_exit%=\n" "tst %[ptr], #15\n" "bne fast_set_4bytes_aligned_loop%=\n" "fast_set_16bytes_aligned_entry%=:\n" "cmp %[count], #4\n" "blt fast_set_16bytes_aligned_exit%=\n" "fast_set_16bytes_aligned_preloop%=:\n" "mov r4, %[value]\n" "mov r5, %[value]\n" "mov r6, %[value]\n" "mov r7, %[value]\n" "fast_set_16bytes_aligned_loop%=:\n" "subs %[count], %[count], #4\n" "stmia %[ptr]!, {r4,r5,r6,r7}\n" "cmp %[count], #4\n" "bge fast_set_16bytes_aligned_loop%=\n" "fast_set_16bytes_aligned_exit%=:\n" "cmp %[count], #0\n" "beq fast_set_exit%=\n" "fast_set_4bytes_aligned_loop_2_%=:\n" "subs %[count], %[count], #1\n" "str %[value], [%[ptr]], #4\n" "bne fast_set_4bytes_aligned_loop_2_%=\n" "fast_set_exit%=:" : [value] "=r"(val), [ptr] "=r"(dest), [count] "=r"(count) : "[value]"(val), "[ptr]"(dest), "[count]"(count) : "r4", "r5", "r6", "r7", "memory", "cc"); #endif } } // namespace LFoundation
willmexe/opuntiaOS
kernel/kernel/tasking/proc.c
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <fs/vfs.h> #include <io/sockets/socket.h> #include <io/tty/tty.h> #include <libkern/bits/errno.h> #include <libkern/libkern.h> #include <libkern/log.h> #include <libkern/syscall_structs.h> #include <mem/kmalloc.h> #include <tasking/elf.h> #include <tasking/proc.h> #include <tasking/sched.h> #include <tasking/tasking.h> #include <tasking/thread.h> static pid_t proc_next_pid = 1; thread_list_t thread_list; int threads_cnt = 0; /** * LOCKLESS */ static ALWAYS_INLINE void proc_kill_all_threads_except_lockless(proc_t* p, thread_t* gthread); static ALWAYS_INLINE void proc_kill_all_threads_lockless(proc_t* p); static ALWAYS_INLINE int proc_setup_lockless(proc_t* p); static ALWAYS_INLINE int proc_setup_tty_lockless(proc_t* p, tty_entry_t* tty); static ALWAYS_INLINE int proc_load_lockless(proc_t* p, thread_t* main_thread, const char* path); static ALWAYS_INLINE int proc_chdir_lockless(proc_t* p, const char* path); static ALWAYS_INLINE file_descriptor_t* proc_get_free_fd_lockless(proc_t* p); static ALWAYS_INLINE file_descriptor_t* proc_get_fd_lockless(proc_t* p, uint32_t index); static ALWAYS_INLINE int proc_is_fd_opened_lockless(file_descriptor_t* fd); /** * THREAD STORAGE */ static thread_list_node_t* proc_alloc_thread_storage_node() { thread_list_node_t* res = (thread_list_node_t*)kmalloc(sizeof(thread_list_node_t)); memset(res->thread_storage, 0, sizeof(res->thread_storage)); res->empty_spots = THREADS_PER_NODE; res->next = NULL; return res; } static thread_t* _proc_alloc_thread() { ASSERT(thread_list.next_empty_node != NULL); lock_acquire(&thread_list.lock); if (!thread_list.next_empty_node->empty_spots) { thread_list_node_t* node = proc_alloc_thread_storage_node(); thread_list.tail->next = node; thread_list.tail = node; thread_list.next_empty_node = node; thread_list.next_empty_index = 0; } for (int i = 0; i < THREADS_PER_NODE; i++) { if (thread_is_freed(&thread_list.next_empty_node->thread_storage[i])) { thread_list.next_empty_node->empty_spots--; thread_list.next_empty_index++; thread_list.next_empty_node->thread_storage[i].status = THREAD_STATUS_ALLOCATED; lock_release(&thread_list.lock); return &thread_list.next_empty_node->thread_storage[i]; } } ASSERT(false); } /** * HELPER FUNCTIONS */ #define foreach_thread(p) \ for (thread_list_node_t* __thread_list_node = thread_list.head; __thread_list_node; __thread_list_node = __thread_list_node->next) \ for (int __i = 0; __i < THREADS_PER_NODE; __i++) \ for (thread_t* thread = &__thread_list_node->thread_storage[__i]; thread && thread_is_alive(thread); thread = NULL) \ if (thread->process->pid == p->pid) thread_t* proc_alloc_thread() { return _proc_alloc_thread(); } thread_t* thread_by_pid(pid_t pid) { thread_list_node_t* __thread_list_node = thread_list.head; while (__thread_list_node) { for (int i = 0; i < THREADS_PER_NODE; i++) { if (__thread_list_node->thread_storage[i].process->pid == pid) { return &__thread_list_node->thread_storage[i]; } } __thread_list_node = __thread_list_node->next; } return NULL; } pid_t proc_alloc_pid() { return atomic_add(&proc_next_pid, 1); } int proc_init_storage() { lock_init(&thread_list.lock); thread_list_node_t* node = proc_alloc_thread_storage_node(); thread_list.head = node; thread_list.tail = node; thread_list.next_empty_node = node; thread_list.next_empty_index = 0; return 0; } /** * INIT FUNCTIONS */ static ALWAYS_INLINE int proc_setup_lockless(proc_t* p) { p->pid = proc_alloc_pid(); p->pgid = p->pid; p->ppid = 0; p->uid = 0; p->gid = 0; p->euid = 0; p->egid = 0; p->suid = 0; p->sgid = 0; p->is_kthread = false; p->main_thread = proc_alloc_thread(); int res = thread_setup_main(p, p->main_thread); if (res != 0) { return res; } /* setting dentries */ p->proc_file = NULL; p->cwd = NULL; /* allocating space for open files */ p->fds = kmalloc(MAX_OPENED_FILES * sizeof(file_descriptor_t)); if (!p->fds) { return -ENOMEM; } memset((void*)p->fds, 0, MAX_OPENED_FILES * sizeof(file_descriptor_t)); /* setting up zones */ if (dynarr_init_of_size(memzone_t, &p->zones, 8) != 0) { return -ENOMEM; } p->status = PROC_ALIVE; p->prio = DEFAULT_PRIO; return 0; } int proc_setup(proc_t* p) { lock_acquire(&p->lock); int res = proc_setup_lockless(p); lock_release(&p->lock); return res; } int proc_setup_with_uid(proc_t* p, uid_t uid, gid_t gid) { lock_acquire(&p->lock); int err = proc_setup_lockless(p); p->uid = uid; p->gid = gid; p->euid = uid; p->egid = gid; p->suid = uid; p->sgid = gid; lock_release(&p->lock); return err; } static ALWAYS_INLINE int proc_setup_tty_lockless(proc_t* p, tty_entry_t* tty) { file_descriptor_t* fd0 = &p->fds[0]; file_descriptor_t* fd1 = &p->fds[1]; file_descriptor_t* fd2 = &p->fds[2]; p->tty = tty; char* path_to_tty = "/dev/tty "; path_to_tty[8] = tty->id + '0'; dentry_t* tty_dentry; if (vfs_resolve_path(path_to_tty, &tty_dentry) < 0) { return -ENOENT; } int err = vfs_open(tty_dentry, fd0, O_RDWR); if (err) { return err; } err = vfs_open(tty_dentry, fd1, O_RDWR); if (err) { return err; } err = vfs_open(tty_dentry, fd2, O_RDWR); if (err) { return err; } dentry_put(tty_dentry); return 0; } int proc_setup_tty(proc_t* p, tty_entry_t* tty) { lock_acquire(&p->lock); int res = proc_setup_tty_lockless(p, tty); lock_release(&p->lock); return res; } int proc_fork_from(proc_t* new_proc, thread_t* from_thread) { proc_t* from_proc = from_thread->process; thread_copy_of(new_proc->main_thread, from_thread); new_proc->ppid = from_proc->pid; new_proc->pgid = from_proc->gid; new_proc->uid = from_proc->uid; new_proc->gid = from_proc->gid; new_proc->euid = from_proc->euid; new_proc->egid = from_proc->egid; new_proc->suid = from_proc->suid; new_proc->sgid = from_proc->sgid; new_proc->cwd = dentry_duplicate(from_proc->cwd); new_proc->proc_file = dentry_duplicate(from_proc->proc_file); new_proc->tty = from_proc->tty; if (from_proc->fds) { for (int i = 0; i < MAX_OPENED_FILES; i++) { if (proc_is_fd_opened_lockless(&from_proc->fds[i])) { file_descriptor_t* fd = &new_proc->fds[i]; proc_copy_fd(&from_proc->fds[i], fd); } } } for (int i = 0; i < from_proc->zones.size; i++) { memzone_t* zone_to_copy = (memzone_t*)dynarr_get(&from_proc->zones, i); if (zone_to_copy->file) { dentry_duplicate(zone_to_copy->file); // For the copied zone. } dynarr_push(&new_proc->zones, zone_to_copy); } return 0; } /** * LOAD FUNCTIONS */ static int _proc_load_bin(proc_t* p, file_descriptor_t* fd) { uint32_t code_size = fd->dentry->inode->size; memzone_t* code_zone = memzone_new_random(p, code_size); code_zone->type = ZONE_TYPE_CODE; code_zone->flags |= ZONE_READABLE | ZONE_EXECUTABLE; /* THIS IS FOR BSS WHICH COULD BE IN THIS ZONE */ code_zone->flags |= ZONE_WRITABLE; memzone_t* bss_zone = memzone_new_random(p, 2 * 4096); bss_zone->type = ZONE_TYPE_DATA; bss_zone->flags |= ZONE_READABLE | ZONE_WRITABLE; memzone_t* stack_zone = memzone_new_random_backward(p, VMM_PAGE_SIZE); stack_zone->type = ZONE_TYPE_STACK; stack_zone->flags |= ZONE_READABLE | ZONE_WRITABLE; /* Copying an exec code */ uint8_t* prog = kmalloc(fd->dentry->inode->size); fd->ops->read(fd->dentry, prog, 0, fd->dentry->inode->size); vmm_copy_to_pdir(p->pdir, prog, code_zone->start, fd->dentry->inode->size); /* Setting registers */ thread_t* main_thread = p->main_thread; set_base_pointer(main_thread->tf, stack_zone->start + VMM_PAGE_SIZE); set_stack_pointer(main_thread->tf, stack_zone->start + VMM_PAGE_SIZE); set_instruction_pointer(main_thread->tf, code_zone->start); kfree(prog); return 0; } static ALWAYS_INLINE int proc_load_lockless(proc_t* p, thread_t* main_thread, const char* path) { int err; file_descriptor_t fd; dentry_t* dentry; if (vfs_resolve_path_start_from(p->cwd, path, &dentry) != 0) { return -ENOENT; } if (vfs_open(dentry, &fd, O_EXEC) != 0) { dentry_put(dentry); return -ENOENT; } // Saving data to restore in case of error. pdirectory_t* old_pdir = p->pdir; dynamic_array_t old_zones = p->zones; // Reallocating proc. pdirectory_t* new_pdir = vmm_new_user_pdir(); vmm_switch_pdir(new_pdir); p->pdir = new_pdir; if (dynarr_init_of_size(memzone_t, &p->zones, 8) != 0) { dentry_put(dentry); vfs_close(&fd); return -ENOMEM; } p->main_thread = main_thread; err = elf_load(p, &fd); if (err) { goto restore; } success: // Clearing proc proc_kill_all_threads_except_lockless(p, p->main_thread); p->pid = p->main_thread->tid; if (p->proc_file) { dentry_put(p->proc_file); } #ifdef FPU_ENABLED fpu_init_state(p->main_thread->fpu_state); #endif if (old_pdir) { vmm_free_pdir(old_pdir, &old_zones); } dynarr_clear(&old_zones); // Setting up proc p->proc_file = dentry; // dentry isn't put, but is transfered to the proc. if (!p->cwd) { p->cwd = dentry_get_parent(p->proc_file); } if (TEST_FLAG(fd.dentry->inode->mode, S_ISUID)) { p->euid = fd.dentry->inode->uid; p->suid = fd.dentry->inode->uid; } if (TEST_FLAG(fd.dentry->inode->mode, S_ISGID)) { p->egid = fd.dentry->inode->gid; p->sgid = fd.dentry->inode->gid; } vfs_close(&fd); return 0; restore: p->pdir = old_pdir; vmm_switch_pdir(old_pdir); vmm_free_pdir(new_pdir, &p->zones); dynarr_clear(&p->zones); p->zones = old_zones; vfs_close(&fd); dentry_put(dentry); return err; } int proc_load(proc_t* p, thread_t* main_thread, const char* path) { lock_acquire(&p->vm_lock); lock_acquire(&p->lock); int res = proc_load_lockless(p, main_thread, path); lock_release(&p->lock); lock_release(&p->vm_lock); return res; } /** * PROC FREE FUNCTIONS */ int proc_free_lockless(proc_t* p) { if (p->status != PROC_DYING || p->pid == 0) { return -ESRCH; } /* closing opend fds */ if (p->fds) { for (int i = 0; i < MAX_OPENED_FILES; i++) { if (proc_is_fd_opened_lockless(&p->fds[i])) { /* think as an active fd */ vfs_close(&p->fds[i]); } } kfree(p->fds); } if (p->proc_file) { dentry_put(p->proc_file); } if (p->cwd) { dentry_put(p->cwd); } /* Key parts deletion. After that line you can't work with this process. */ proc_kill_all_threads_lockless(p); if (!p->is_kthread) { vmm_free_pdir(p->pdir, &p->zones); p->pdir = NULL; } p->is_tracee = false; dynarr_free(&p->zones); return 0; } int proc_free(proc_t* p) { lock_acquire(&p->vm_lock); lock_acquire(&p->lock); int res = proc_free_lockless(p); lock_release(&p->lock); lock_release(&p->vm_lock); return res; } int proc_die(proc_t* p) { lock_acquire(&p->lock); p->status = PROC_DYING; foreach_thread(p) { thread_die(thread); } tasking_evict_zombies_waiting_for(p); lock_release(&p->lock); return 0; } int proc_block_all_threads(proc_t* p, const blocker_t* blocker) { lock_acquire(&p->lock); foreach_thread(p) { thread_init_blocker(thread, blocker); } lock_release(&p->lock); return 0; } /** * PROC THREAD FUNCTIONS */ thread_t* proc_create_thread(proc_t* p) { lock_acquire(&p->lock); thread_t* thread = proc_alloc_thread(); thread_setup(p, thread); sched_enqueue(thread); lock_release(&p->lock); return thread; } static ALWAYS_INLINE void proc_kill_all_threads_except_lockless(proc_t* p, thread_t* gthread) { foreach_thread(p) { if (!gthread || thread->tid != gthread->tid) { thread_free(thread); } } } static ALWAYS_INLINE void proc_kill_all_threads_lockless(proc_t* p) { proc_kill_all_threads_except_lockless(p, NULL); } void proc_kill_all_threads_except(proc_t* p, thread_t* gthread) { lock_acquire(&p->lock); proc_kill_all_threads_except_lockless(p, gthread); lock_release(&p->lock); } void proc_kill_all_threads(proc_t* p) { proc_kill_all_threads_except(p, NULL); } /** * PROC FS FUNCTIONS */ static ALWAYS_INLINE int proc_chdir_lockless(proc_t* p, const char* path) { char* kpath = NULL; if (!str_validate_len(path, 128)) { return -EINVAL; } kpath = kmem_bring_to_kernel(path, strlen(path) + 1); dentry_t* new_cwd = NULL; int ret = vfs_resolve_path_start_from(p->cwd, kpath, &new_cwd); if (ret) { return -ENOENT; } if (!dentry_inode_test_flag(new_cwd, S_IFDIR)) { dentry_put(new_cwd); return -ENOTDIR; } /* Put an old one back */ if (p->cwd) { dentry_put(p->cwd); } p->cwd = new_cwd; return 0; } int proc_chdir(proc_t* p, const char* path) { lock_acquire(&p->lock); int res = proc_chdir_lockless(p, path); lock_release(&p->lock); return res; } int proc_get_fd_id(proc_t* p, file_descriptor_t* fd) { lock_acquire(&p->lock); ASSERT(p->fds); /* Calculating id with pointers */ uintptr_t start = (uintptr_t)p->fds; uintptr_t fd_ptr = (uintptr_t)fd; fd_ptr -= start; int fd_res = fd_ptr / sizeof(file_descriptor_t); if (!(fd_ptr % sizeof(file_descriptor_t))) { lock_release(&p->lock); return fd_res; } lock_release(&p->lock); return -1; } static ALWAYS_INLINE int proc_is_fd_opened_lockless(file_descriptor_t* fd) { return fd->dentry != NULL; } static ALWAYS_INLINE file_descriptor_t* proc_get_free_fd_lockless(proc_t* p) { ASSERT(p->fds); for (int i = 0; i < MAX_OPENED_FILES; i++) { if (!proc_is_fd_opened_lockless(&p->fds[i])) { lock_init(&p->fds[i].lock); return &p->fds[i]; } } return NULL; } file_descriptor_t* proc_get_free_fd(proc_t* p) { lock_acquire(&p->lock); file_descriptor_t* res = proc_get_free_fd_lockless(p); lock_release(&p->lock); return res; } static ALWAYS_INLINE file_descriptor_t* proc_get_fd_lockless(proc_t* p, uint32_t index) { ASSERT(p->fds); if (index >= MAX_OPENED_FILES) { return NULL; } if (!proc_is_fd_opened_lockless(&p->fds[index])) { return NULL; } return &p->fds[index]; } file_descriptor_t* proc_get_fd(proc_t* p, uint32_t index) { lock_acquire(&p->lock); file_descriptor_t* res = proc_get_fd_lockless(p, index); lock_release(&p->lock); return res; } int proc_copy_fd(file_descriptor_t* oldfd, file_descriptor_t* newfd) { lock_acquire(&oldfd->lock); if (oldfd->type == FD_TYPE_FILE) { newfd->type = FD_TYPE_FILE; newfd->dentry = dentry_duplicate(oldfd->dentry); newfd->offset = oldfd->offset; newfd->flags = oldfd->flags; newfd->ops = oldfd->ops; lock_init(&newfd->lock); lock_release(&oldfd->lock); return 0; } else if (oldfd->type == FD_TYPE_SOCKET) { newfd->type = FD_TYPE_SOCKET; newfd->sock_entry = socket_duplicate(oldfd->sock_entry); newfd->offset = oldfd->offset; newfd->flags = oldfd->flags; newfd->ops = oldfd->ops; lock_init(&newfd->lock); lock_release(&oldfd->lock); return 0; } lock_release(&oldfd->lock); return -1; }
willmexe/opuntiaOS
kernel/include/mem/memzone.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_MEM_MEMZONE_H #define _KERNEL_MEM_MEMZONE_H #include <algo/dynamic_array.h> #include <fs/vfs.h> #include <libkern/types.h> #include <mem/bits/zone.h> struct vm_ops; struct memzone { uintptr_t start; size_t len; uint32_t type; uint32_t flags; dentry_t* file; uintptr_t offset; struct vm_ops* ops; }; typedef struct memzone memzone_t; // TODO: Make it not depandent on proc. struct proc; memzone_t* memzone_new(struct proc* p, size_t start, size_t len); memzone_t* memzone_extend(struct proc* proc, size_t start, size_t len); memzone_t* memzone_new_random(struct proc* p, size_t len); memzone_t* memzone_new_random_backward(struct proc* p, size_t len); memzone_t* memzone_find(struct proc* p, size_t addr); memzone_t* memzone_find_no_proc(dynamic_array_t* zones, size_t addr); int memzone_free_no_proc(dynamic_array_t*, memzone_t*); int memzone_free(struct proc*, memzone_t*); #endif // _KERNEL_MEM_MEMZONE_H
willmexe/opuntiaOS
libs/libui/include/libui/Layer.h
<filename>libs/libui/include/libui/Layer.h /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include <libg/Font.h> #include <libui/Constants/Text.h> #include <libui/EdgeInsets.h> #include <libui/View.h> #include <string> namespace UI { class Layer { public: Layer() = default; ~Layer() = default; void set_corner_mask(const LG::CornerMask& cm) { m_corner_mask = cm; } const LG::CornerMask& corner_mask() const { return m_corner_mask; } private: LG::CornerMask m_corner_mask {}; }; } // namespace UI
willmexe/opuntiaOS
libs/libc/stdlib/pts.c
<filename>libs/libc/stdlib/pts.c<gh_stars>100-1000 #include <errno.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #define _PATH_PTS "/dev/pts" #define MASTER_PTY(dev) (major(dev) == 128) #define SLAVE_PTY(dev) (major(dev) == 136) int posix_openpt(int flags) { return open("/dev/ptmx", flags); } int ptsname_r(int fd, char* buf, size_t buflen) { int len = strlen(_PATH_PTS); if (buflen < len + 2) { set_errno(ERANGE); return -ERANGE; } fstat_t stat; if (fstat(fd, &stat) < 0) { return errno; } if (!MASTER_PTY(stat.dev)) { set_errno(ENOTTY); return -ENOTTY; } int ptyno = minor(stat.dev); char* p = strcpy(buf, _PATH_PTS); p[len + 0] = '0' + ptyno; p[len + 1] = '\0'; return 0; } static char ptsbuf[32]; char* ptsname(int fd) { if (ptsname_r(fd, ptsbuf, sizeof(ptsbuf)) < 0) { return NULL; } return ptsbuf; }
willmexe/opuntiaOS
kernel/include/tasking/bits/sched.h
<filename>kernel/include/tasking/bits/sched.h /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_TASKING_BITS_SCHED_H #define _KERNEL_TASKING_BITS_SCHED_H #define MAX_PRIO 0 #define MIN_PRIO 11 #define IDLE_PRIO (MIN_PRIO + 1) #define PROC_PRIOS_COUNT (MIN_PRIO - MAX_PRIO + 1) #define TOTAL_PRIOS_COUNT (IDLE_PRIO - MAX_PRIO + 1) #define DEFAULT_PRIO 6 #define SCHED_INT 10 #define LAST_CPU_NOT_SET 0xffff struct thread; struct runqueue { struct thread* head; struct thread* tail; }; typedef struct runqueue runqueue_t; struct sched_data { int next_read_prio; runqueue_t* master_buf; runqueue_t* slave_buf; int enqueued_tasks; }; typedef struct sched_data sched_data_t; #endif // _KERNEL_TASKING_BITS_SCHED_H
willmexe/opuntiaOS
libs/libc/include/bits/time.h
#ifndef _LIBC_BITS_TIME_H #define _LIBC_BITS_TIME_H #include <sys/cdefs.h> #include <sys/types.h> __BEGIN_DECLS struct timeval { time_t tv_sec; uint32_t tv_usec; }; typedef struct timeval timeval_t; #define DST_NONE 0 /* not on dst */ #define DST_USA 1 /* USA style dst */ #define DST_AUST 2 /* Australian style dst */ #define DST_WET 3 /* Western European dst */ #define DST_MET 4 /* Middle European dst */ #define DST_EET 5 /* Eastern European dst */ #define DST_CAN 6 /* Canada */ struct timezone { int tz_minuteswest; /* minutes west of Greenwich */ int tz_dsttime; /* type of dst correction */ }; typedef struct timezone timezone_t; struct timespec { time_t tv_sec; uint32_t tv_nsec; }; typedef struct timespec timespec_t; struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; typedef struct tm tm_t; typedef enum { CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID, } clockid_t; __END_DECLS #endif // _LIBC_BITS_TIME_H
willmexe/opuntiaOS
servers/window_server/src/SystemApps/SystemApp.h
<filename>servers/window_server/src/SystemApps/SystemApp.h /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include "../Target/Generic/Window.h" namespace WinServer { class SystemApp { public: SystemApp() = default; ~SystemApp() = default; void set_window(Window* win) { m_window = win; } Window* window() { return m_window; } const Window* window() const { return m_window; } bool has_value() const { return !!m_window; } operator bool() const { return has_value(); } private: bool visible; Window* m_window {}; }; }
willmexe/opuntiaOS
userland/system/applist/AppListWindow.h
#pragma once #include <libg/Size.h> #include <libui/Screen.h> #include <libui/Window.h> class AppListWindow : public UI::Window { public: AppListWindow(const LG::Size& size) : UI::Window("AppList", size, UI::WindowType::AppList) { } void receive_event(std::unique_ptr<LFoundation::Event> event) override; };
willmexe/opuntiaOS
kernel/include/libkern/ctype.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_LIBKERN_CTYPE_H #define _KERNEL_LIBKERN_CTYPE_H #include <libkern/types.h> #define _U 01 #define _L 02 #define _N 04 #define _S 010 #define _P 020 #define _C 040 #define _X 0100 #define _B 0200 extern const char __kern_ctypes[256]; static inline int isalnum(int c) { return __kern_ctypes[(unsigned char)(c)] & (_U | _L | _N); } static inline int isalpha(int c) { return __kern_ctypes[(unsigned char)(c)] & (_U | _L); } static inline int iscntrl(int c) { return __kern_ctypes[(unsigned char)(c)] & (_C); } static inline int isdigit(int c) { return __kern_ctypes[(unsigned char)(c)] & (_N); } static inline int isxdigit(int c) { return __kern_ctypes[(unsigned char)(c)] & (_N | _X); } static inline int isspace(int c) { return __kern_ctypes[(unsigned char)(c)] & (_S); } static inline int ispunct(int c) { return __kern_ctypes[(unsigned char)(c)] & (_P); } static inline int isprint(int c) { return __kern_ctypes[(unsigned char)(c)] & (_P | _U | _L | _N | _B); } static inline int isgraph(int c) { return __kern_ctypes[(unsigned char)(c)] & (_P | _U | _L | _N); } static inline int islower(int c) { return (__kern_ctypes[(unsigned char)(c)] & (_U | _L)) == _L; } static inline int isupper(int c) { return (__kern_ctypes[(unsigned char)(c)] & (_U | _L)) == _U; } static inline int tolower(int c) { return isupper(c) ? (c) - 'a' + 'A' : c; } static inline int toupper(int c) { return islower(c) ? (c) - 'A' + 'a' : c; } #endif /* _KERNEL_LIBKERN_CTYPE_H */
willmexe/opuntiaOS
userland/utilities/ls/main.c
#include <dirent.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #define BUF_SIZE 1024 struct linux_dirent { uint32_t inode; uint16_t rec_len; uint8_t name_len; uint8_t file_type; char* name; }; int main(int argc, char** argv) { int fd, nread; char buf[BUF_SIZE]; struct linux_dirent* d; int bpos; char d_type; char has_path = 0; char show_inodes = 0; char show_private = 0; for (int i = 1; i < argc; i++) { if (memcmp(argv[i], "-i", 3) == 0) { show_inodes = 1; } else if (memcmp(argv[i], "-a", 3) == 0) { show_private = 1; } else { has_path = 1; } } if (has_path) { fd = open(argv[1], O_RDONLY | O_DIRECTORY); } else { fd = open(".", O_RDONLY | O_DIRECTORY); } if (fd < 0) { printf("ls: can't open file\n"); return -1; } for (;;) { nread = getdents(fd, buf, BUF_SIZE); if (nread < 0) { printf("ls: can't read dir\n"); return -1; } if (nread == 0) break; for (bpos = 0; bpos < nread;) { d = (struct linux_dirent*)(buf + bpos); if (((char*)&d->name)[0] != '.' || show_private) { printf("%s", (char*)&d->name); if (show_inodes) { printf(" %d", d->inode); } printf("\n"); } bpos += d->rec_len; } } return 0; }
willmexe/opuntiaOS
kernel/include/mem/vmm.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_MEM_VMM_H #define _KERNEL_MEM_VMM_H #include <libkern/libkern.h> #include <mem/bits/mmu.h> #include <mem/bits/swap.h> #include <mem/bits/vm.h> #include <mem/pmm.h> #include <platform/generic/vmm/consts.h> #include <platform/generic/vmm/pde.h> #include <platform/generic/vmm/pte.h> #define vmm_is_kernel_address(add) (add >= KERNEL_BASE) #define IS_KERNEL_VADDR(vaddr) (vaddr >= KERNEL_BASE) #define IS_USER_VADDR(vaddr) (vaddr < KERNEL_BASE) struct memzone; struct vm_ops { int (*load_page_content)(struct memzone* zone, uintptr_t vaddr); int (*swap_page_mode)(struct memzone* zone, uintptr_t vaddr); int (*restore_swapped_page)(struct memzone* zone, uintptr_t vaddr); }; typedef struct vm_ops vm_ops_t; /** * PUBLIC FUNCTIONS */ struct dynamic_array; int vmm_setup(); int vmm_setup_secondary_cpu(); int vmm_free_pdir(pdirectory_t* pdir, struct dynamic_array* zones); int vmm_alloc_page(uintptr_t vaddr, uint32_t settings); int vmm_tune_page(uintptr_t vaddr, uint32_t settings); int vmm_tune_pages(uintptr_t vaddr, size_t length, uint32_t settings); int vmm_free_page(uintptr_t vaddr, page_desc_t* page, struct dynamic_array* zones); int vmm_map_page(uintptr_t vaddr, uintptr_t paddr, uint32_t settings); int vmm_map_pages(uintptr_t vaddr, uintptr_t paddr, size_t n_pages, uint32_t settings); int vmm_unmap_page(uintptr_t vaddr); int vmm_unmap_pages(uintptr_t vaddr, size_t n_pages); int vmm_copy_page(uintptr_t to_vaddr, uintptr_t src_vaddr, ptable_t* src_ptable); int vmm_swap_page(ptable_t* ptable, struct memzone* zone, uintptr_t vaddr); int vmm_map_page_lockless(uintptr_t vaddr, uintptr_t paddr, uint32_t settings); int vmm_map_pages_lockless(uintptr_t vaddr, uintptr_t paddr, size_t n_pages, uint32_t settings); int vmm_unmap_page_lockless(uintptr_t vaddr); int vmm_unmap_pages_lockless(uintptr_t vaddr, size_t n_pages); int vmm_copy_page_lockless(uintptr_t to_vaddr, uintptr_t src_vaddr, ptable_t* src_ptable); pdirectory_t* vmm_new_user_pdir(); pdirectory_t* vmm_new_forked_user_pdir(); void* vmm_bring_to_kernel(uint8_t* src, size_t length); void vmm_prepare_active_pdir_for_writing_at(uintptr_t dest_vaddr, size_t length); void vmm_copy_to_user(void* dest, void* src, size_t length); void vmm_copy_to_pdir(pdirectory_t* pdir, void* src, uintptr_t dest_vaddr, size_t length); pdirectory_t* vmm_get_active_pdir(); pdirectory_t* vmm_get_kernel_pdir(); int vmm_switch_pdir(pdirectory_t* pdir); int vmm_page_fault_handler(uint32_t info, uintptr_t vaddr); inline static table_desc_t* _vmm_pdirectory_lookup(pdirectory_t* pdir, uintptr_t vaddr) { if (pdir) { return &pdir->entities[VMM_OFFSET_IN_DIRECTORY(vaddr)]; } return 0; } inline static page_desc_t* _vmm_ptable_lookup(ptable_t* ptable, uintptr_t vaddr) { if (ptable) { return &ptable->entities[VMM_OFFSET_IN_TABLE(vaddr)]; } return 0; } #endif // _KERNEL_MEM_VMM_H
willmexe/opuntiaOS
kernel/include/platform/x86/init.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_PLATFORM_X86_INIT_H #define _KERNEL_PLATFORM_X86_INIT_H #include <libkern/types.h> void platform_init_boot_cpu(); void platform_setup_boot_cpu(); void platform_setup_secondary_cpu(); #endif /* _KERNEL_PLATFORM_X86_INIT_H */
willmexe/opuntiaOS
userland/utilities/cat/main.c
#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define BUF_SIZE 512 char buf[BUF_SIZE]; void cat(int fd) { int n = 0; while ((n = read(fd, buf, sizeof(buf))) > 0) { if (fwrite(buf, n, 1, stdout) != n) { exit(1); } } } int main(int argc, char** argv) { int fd, i; if (argc <= 1) { cat(0); return 0; } for (i = 1; i < argc; i++) { if ((fd = open(argv[i], O_RDONLY)) < 0) { printf("cat: cannot open %s\n", argv[i]); return 1; } cat(fd); close(fd); } return 0; }
willmexe/opuntiaOS
libs/libui/include/libui/AppDelegate.h
<filename>libs/libui/include/libui/AppDelegate.h /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include <libfoundation/Event.h> #include <libfoundation/EventLoop.h> #include <libfoundation/EventReceiver.h> #include <libg/Size.h> #include <libui/Connection.h> #include <libui/Window.h> #include <memory> #include <sys/types.h> #define SET_APP_DELEGATE(name) \ name* MainAppDelegatePtr; \ extern "C" bool __init_app_delegate(UI::AppDelegate** res) \ { \ MainAppDelegatePtr = new name(); \ *res = MainAppDelegatePtr; \ return MainAppDelegatePtr->application(); \ } namespace UI { class AppDelegate { public: AppDelegate() = default; virtual ~AppDelegate() = default; LG::Size window_size() const { #ifdef TARGET_DESKTOP return preferred_desktop_window_size(); #elif TARGET_MOBILE return LG::Size(320, 548); #endif } virtual LG::Size preferred_desktop_window_size() const { return LG::Size(400, 300); } virtual const char* icon_path() const { return "/res/icons/apps/missing.icon"; } virtual bool application() { return false; } virtual void application_will_terminate() { } private: }; } // namespace UI
willmexe/opuntiaOS
userland/applications/activity_monitor/ViewController.h
<gh_stars>100-1000 /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include "GraphView.h" #include <libfoundation/ProcessInfo.h> #include <libui/App.h> #include <libui/Button.h> #include <libui/Label.h> #include <libui/StackView.h> #include <libui/View.h> #include <libui/ViewController.h> #include <libui/Window.h> #include <memory> #include <sys/types.h> static char buf[256]; class ViewController : public UI::ViewController<UI::View> { public: ViewController(UI::View& view) : UI::ViewController<UI::View>(view) , m_cpu_count(LFoundation::ProcessInfo::the().processor_count()) { } virtual ~ViewController() = default; inline int cpu_count() const { return m_cpu_count; } void view_did_load() override { state.cpu_load.resize(cpu_count()); state.cpu_old_user_time.resize(cpu_count()); state.cpu_old_system_time.resize(cpu_count()); state.cpu_old_idle_time.resize(cpu_count()); view().set_background_color(LG::Color::LightSystemBackground); auto& label = view().add_subview<UI::Label>(LG::Rect(0, 0, 16, 24)); label.set_text_color(LG::Color::DarkSystemText); label.set_text("Monitor"); label.set_font(LG::Font::system_font(LG::Font::SystemTitleSize)); label.set_width(label.preferred_width()); auto& cpu_label = view().add_subview<UI::Label>(LG::Rect(0, 0, 180, 16)); auto& cpu_graphs_stackview = view().add_subview<UI::StackView>(LG::Rect(0, 0, 184, 100)); cpu_graphs_stackview.set_distribution(UI::StackView::Distribution::FillEqually); cpu_graphs_stackview.set_spacing(10); view().add_constraint(UI::Constraint(label, UI::Constraint::Attribute::Left, UI::Constraint::Relation::Equal, UI::SafeArea::Left)); view().add_constraint(UI::Constraint(label, UI::Constraint::Attribute::Top, UI::Constraint::Relation::Equal, UI::SafeArea::Top)); view().add_constraint(UI::Constraint(cpu_label, UI::Constraint::Attribute::Left, UI::Constraint::Relation::Equal, UI::SafeArea::Left)); view().add_constraint(UI::Constraint(cpu_label, UI::Constraint::Attribute::Top, UI::Constraint::Relation::Equal, label, UI::Constraint::Attribute::Bottom, 1, UI::Padding::AfterTitle)); view().add_constraint(UI::Constraint(cpu_graphs_stackview, UI::Constraint::Attribute::Left, UI::Constraint::Relation::Equal, UI::SafeArea::Left)); view().add_constraint(UI::Constraint(cpu_graphs_stackview, UI::Constraint::Attribute::Right, UI::Constraint::Relation::Equal, UI::SafeArea::Right)); view().add_constraint(UI::Constraint(cpu_graphs_stackview, UI::Constraint::Attribute::Top, UI::Constraint::Relation::Equal, cpu_label, UI::Constraint::Attribute::Bottom, 1, 8)); view().add_constraint(UI::Constraint(cpu_graphs_stackview, UI::Constraint::Attribute::Bottom, UI::Constraint::Relation::Equal, UI::SafeArea::Bottom)); for (int i = 0; i < cpu_count(); i++) { auto& graph = cpu_graphs_stackview.add_arranged_subview<GraphView>(200); cpu_graphs_stackview.add_constraint(UI::Constraint(graph, UI::Constraint::Attribute::Height, UI::Constraint::Relation::Equal, cpu_graphs_stackview, UI::Constraint::Attribute::Height, 1, 0)); cpu_graphs.push_back(&graph); } view().set_needs_layout(); UI::App::the().event_loop().add(LFoundation::Timer([&] { update_data(); cpu_label.set_text(std::string("Load ") + std::to_string(state.cpu_load[0]) + "%"); cpu_label.set_needs_display(); }, 1000, LFoundation::Timer::Repeat)); } int update_cpu_load() { int fd_proc_stat = open("/proc/stat", O_RDONLY); int offset = 0; read(fd_proc_stat, buf, sizeof(buf)); for (int i = 0; i < cpu_count(); i++) { int user_time, system_time, idle_time; int num; offset += sscanf(buf + offset, "cpu%d %d 0 %d %d\n", &num, &user_time, &system_time, &idle_time); int diff_user_time = user_time - state.cpu_old_user_time[i]; int diff_system_time = system_time - state.cpu_old_system_time[i]; int diff_idle_time = idle_time - state.cpu_old_idle_time[i]; state.cpu_old_user_time[i] = user_time; state.cpu_old_system_time[i] = system_time; state.cpu_old_idle_time[i] = idle_time; if (diff_user_time + diff_system_time + diff_idle_time == 0) { state.cpu_load[i] = 0; } else { state.cpu_load[i] = (diff_user_time + diff_system_time) * 100 / (diff_user_time + diff_system_time + diff_idle_time); } cpu_graphs[i]->add_new_value(state.cpu_load[i]); } close(fd_proc_stat); return 0; } void update_data() { update_cpu_load(); } private: int m_cpu_count; int fd_proc_stat; std::vector<GraphView*> cpu_graphs; struct State { std::vector<int> cpu_load; std::vector<int> cpu_old_user_time; std::vector<int> cpu_old_system_time; std::vector<int> cpu_old_idle_time; }; State state; };
willmexe/opuntiaOS
servers/window_server/shared/MessageContent/MouseAction.h
#pragma once enum MouseActionType { LeftMouseButtonPressed, LeftMouseButtonReleased, RightMouseButtonPressed, RightMouseButtonReleased, }; class MouseActionState { public: MouseActionState() = default; ~MouseActionState() = default; inline int state() const { return m_state; } inline void set(MouseActionType state) { m_state |= (int)state; } private: int m_state { 0 }; };
willmexe/opuntiaOS
libs/libc/include/stdbool.h
<reponame>willmexe/opuntiaOS #ifndef _LIBC_STDBOOL_H #define _LIBC_STDBOOL_H #include <sys/cdefs.h> __BEGIN_DECLS #ifndef __bool_true_false_are_defined #define bool _Bool #define true (1) #define false (0) #define __bool_true_false_are_defined 1 #endif // __bool_true_false_are_defined __END_DECLS #endif //_LIBC_STDBOOL_H
willmexe/opuntiaOS
libs/libipc/include/libipc/Encoder.h
#pragma once #include <vector> typedef std::vector<uint8_t> EncodedMessage; class Encoder { public: ~Encoder() = default; static void append(EncodedMessage& buf, int val) { buf.push_back((uint8_t)val); buf.push_back((uint8_t)(val >> 8)); buf.push_back((uint8_t)(val >> 16)); buf.push_back((uint8_t)(val >> 24)); } static void append(EncodedMessage& buf, unsigned int val) { buf.push_back((uint8_t)val); buf.push_back((uint8_t)(val >> 8)); buf.push_back((uint8_t)(val >> 16)); buf.push_back((uint8_t)(val >> 24)); } static void append(EncodedMessage& buf, unsigned long val) { buf.push_back((uint8_t)val); buf.push_back((uint8_t)(val >> 8)); buf.push_back((uint8_t)(val >> 16)); buf.push_back((uint8_t)(val >> 24)); } static void decode(const char* buf, size_t& offset, unsigned long& val) { uint8_t b0 = buf[offset++]; uint8_t b1 = buf[offset++]; uint8_t b2 = buf[offset++]; uint8_t b3 = buf[offset++]; val = 0; val |= (uint8_t(b3) << 24); val |= (uint8_t(b2) << 16); val |= (uint8_t(b1) << 8); val |= (uint8_t(b0)); } static void decode(const char* buf, size_t& offset, unsigned int& val) { uint8_t b0 = buf[offset++]; uint8_t b1 = buf[offset++]; uint8_t b2 = buf[offset++]; uint8_t b3 = buf[offset++]; val = 0; val |= (uint8_t(b3) << 24); val |= (uint8_t(b2) << 16); val |= (uint8_t(b1) << 8); val |= (uint8_t(b0)); } static void decode(const char* buf, size_t& offset, int& val) { uint8_t b0 = buf[offset++]; uint8_t b1 = buf[offset++]; uint8_t b2 = buf[offset++]; uint8_t b3 = buf[offset++]; val = 0; val |= (uint8_t(b3) << 24); val |= (uint8_t(b2) << 16); val |= (uint8_t(b1) << 8); val |= (uint8_t(b0)); } template <typename T> static void append(EncodedMessage& buf, T& value) { value.encode(buf); } template <typename T> static void decode(const char* buf, size_t& offset, T& value) { value.decode(buf, offset); } private: Encoder() = default; };
willmexe/opuntiaOS
kernel/kernel/algo/hash.c
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <algo/hash.h> uint32_t hash_crc32(uint8_t* data, size_t len) { uint32_t byte, mask; uint32_t crc = 0xFFFFFFFF; for (size_t i = 0; i < len; i++) { byte = data[i]; crc ^= byte; for (int j = 0; j < 8; j++) { mask = -(crc & 1); crc = (crc >> 1) ^ (0xEDB88320 & mask); } } return ~crc; } uint32_t hashstr_crc32(char* data) { uint32_t byte, mask; uint32_t crc = 0xFFFFFFFF; for (size_t i = 0; data[i]; i++) { byte = data[i]; crc ^= byte; for (int j = 0; j < 8; j++) { mask = -(crc & 1); crc = (crc >> 1) ^ (0xEDB88320 & mask); } } return ~crc; }
willmexe/opuntiaOS
libs/libc/include/sys/cdefs.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * + Contributed by bellrise <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _LIBC_SYS_CDEFS_H #define _LIBC_SYS_CDEFS_H #if defined(__cplusplus) #define __BEGIN_DECLS extern "C" { #define __END_DECLS } #else #define __BEGIN_DECLS #define __END_DECLS #endif /* Define __use_instead macro for some functions so the user can be warned about better/faster functions. */ #ifndef __use_instead #ifdef __clang__ #define __use_instead(F) __attribute__((diagnose_if(1, "use " F " instead", \ "warning"))) #elif defined(__GNUC__) #define __use_instead(F) __attribute__((warning("use " F " instead"))) #endif #endif #endif // _LIBC_SYS_CDEFS_H
willmexe/opuntiaOS
libs/libc/include/termios.h
<reponame>willmexe/opuntiaOS #ifndef _LIBC_TERMIOS_H #define _LIBC_TERMIOS_H #include <sys/_structs.h> #include <sys/cdefs.h> #include <sys/types.h> __BEGIN_DECLS typedef unsigned char cc_t; typedef unsigned int speed_t; typedef unsigned int tcflag_t; #define NCCS 32 struct termios { tcflag_t c_iflag; /* input mode flags */ tcflag_t c_oflag; /* output mode flags */ tcflag_t c_cflag; /* control mode flags */ tcflag_t c_lflag; /* local mode flags */ cc_t c_cc[NCCS]; /* control characters */ }; typedef struct termios termios_t; /* c_cc characters */ #define VINTR 0 #define VQUIT 1 #define VERASE 2 #define VKILL 3 #define VEOF 4 #define VTIME 5 #define VMIN 6 #define VSWTC 7 #define VSTART 8 #define VSTOP 9 #define VSUSP 10 #define VEOL 11 #define VREPRINT 12 #define VDISCARD 13 #define VWERASE 14 #define VLNEXT 15 #define VEOL2 16 /* c_lflag bits */ #define ISIG 0000001 #define ICANON 0000002 #define ECHO 0000010 #define ECHOE 0000020 #define ECHOK 0000040 #define ECHONL 0000100 #define NOFLSH 0000200 #define TOSTOP 0000400 #define IEXTEN 0100000 /* tcflow() and TCXONC use these */ #define TCOOFF 0 #define TCOON 1 #define TCIOFF 2 #define TCION 3 /* tcflush() and TCFLSH use these */ #define TCIFLUSH 0 #define TCOFLUSH 1 #define TCIOFLUSH 2 /* tcsetattr uses these */ #define TCSANOW 0 #define TCSADRAIN 1 #define TCSAFLUSH 2 int tcgetattr(int fd, termios_t* termios_p); int tcsetattr(int fd, int optional_actions, const termios_t* termios_p); __END_DECLS #endif /* _LIBC_TERMIOS_H */
willmexe/opuntiaOS
kernel/include/libkern/bits/sys/select.h
<reponame>willmexe/opuntiaOS #ifndef _KERNEL_LIBKERN_BITS_SYS_SELECT_H #define _KERNEL_LIBKERN_BITS_SYS_SELECT_H #include <libkern/types.h> #define FD_SETSIZE 8 struct fd_set { uint8_t fds[FD_SETSIZE / 8]; }; typedef struct fd_set fd_set_t; #define FD_SET(fd, fd_set_ptr) ((fd_set_ptr)->fds[fd / 8] |= (1 << (fd % 8))) #define FD_CLR(fd, fd_set_ptr) ((fd_set_ptr)->fds[fd / 8] &= ~(1 << (fd % 8))) #define FD_ZERO(fd_set_ptr) (memset((uint8_t*)(fd_set_ptr), 0, sizeof(fd_set_t))) #define FD_ISSET(fd, fd_set_ptr) ((fd_set_ptr)->fds[fd / 8] & (1 << (fd % 8))) #endif // _KERNEL_LIBKERN_BITS_SYS_SELECT_H
willmexe/opuntiaOS
kernel/include/platform/generic/tasking/trapframe.h
#ifdef __i386__ #include <platform/x86/tasking/trapframe.h> #elif __arm__ #include <platform/aarch32/tasking/trapframe.h> #endif
willmexe/opuntiaOS
kernel/kernel/platform/aarch32/target/cortex-a15/mapping_layout.c
<reponame>willmexe/opuntiaOS /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <mem/vmm.h> #include <platform/aarch32/target/cortex-a15/device_settings.h> #include <platform/aarch32/target/cortex-a15/memmap.h> #include <platform/generic/vmm/mapping_table.h> mapping_entry_t kernel_mapping_table[] = { { .paddr = KERNEL_PM_BASE + 0x000000, .vaddr = KERNEL_BASE + 0x000000, .flags = 0, .pages = 1, .last = 0 }, { .paddr = KERNEL_PM_BASE + 0x100000, .vaddr = KERNEL_BASE + 0x100000, .flags = 0, .pages = 1, .last = 0 }, { .paddr = KERNEL_PM_BASE + 0x200000, .vaddr = KERNEL_BASE + 0x200000, .flags = 0, .pages = 1, .last = 0 }, { .paddr = KERNEL_PM_BASE + 0x300000, .vaddr = KERNEL_BASE + 0x300000, .flags = 0, .pages = 1, .last = 1 }, }; mapping_entry_t extern_mapping_table[] = { { .paddr = UART_BASE, .vaddr = UART_BASE, .flags = MMU_FLAG_PERM_READ | MMU_FLAG_PERM_WRITE | MMU_FLAG_PERM_EXEC, .pages = 1, .last = 1 }, };
willmexe/opuntiaOS
userland/system/homescreen/AppListView.h
<gh_stars>100-1000 #pragma once #include <libg/Font.h> #include <libui/Label.h> #include <libui/PopupMenu.h> #include <libui/View.h> #include <list> #include <string> #include <unistd.h> class AppListView : public UI::View { UI_OBJECT(); static constexpr int INVALID = -1; public: AppListView(View* superview, const LG::Rect& frame); void set_target_window_id(int winid) { m_target_window_id = winid; } void display(const LG::Rect& rect) override; void mouse_down(const LG::Point<int>& location) override { on_click(); } private: void on_click(); int m_target_window_id { INVALID }; LG::PixelBitmap m_icon; };
willmexe/opuntiaOS
libs/libc/include/sys/types.h
<filename>libs/libc/include/sys/types.h #ifndef _LIBC_SYS_TYPES_H #define _LIBC_SYS_TYPES_H #include <sys/_types/_devs.h> #include <sys/_types/_ints.h> #include <sys/_types/_va_list.h> #endif /* _LIBC_SYS_TYPES_H */
willmexe/opuntiaOS
kernel/kernel/platform/x86/vmm/pte.c
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <mem/vmm.h> #include <platform/x86/vmm/pte.h> void page_desc_init(page_desc_t* pte) { *pte = 0; } uint32_t page_desc_mmu_flags_to_arch_flags(uint32_t attrs) { return attrs; } void page_desc_set_attrs(page_desc_t* pte, uint32_t attrs) { *pte |= attrs; } void page_desc_del_attrs(page_desc_t* pte, uint32_t attrs) { *pte &= ~(attrs); } bool page_desc_has_attrs(page_desc_t pte, uint32_t attrs) { return ((pte & attrs) > 0); } void page_desc_set_frame(page_desc_t* pte, uint32_t frame) { page_desc_del_frame(pte); frame >>= PAGE_DESC_FRAME_OFFSET; *pte |= (frame << PAGE_DESC_FRAME_OFFSET); } void page_desc_del_frame(page_desc_t* pte) { *pte &= ((1 << (PAGE_DESC_FRAME_OFFSET)) - 1); } bool page_desc_is_present(page_desc_t pte) { return ((pte & PAGE_DESC_PRESENT) > 0); } bool page_desc_is_writable(page_desc_t pte) { return ((pte & PAGE_DESC_WRITABLE) > 0); } bool page_desc_is_user(page_desc_t pte) { return ((pte & PAGE_DESC_USER) > 0); } bool page_desc_is_not_cacheable(page_desc_t pte) { return ((pte & PAGE_DESC_NOT_CACHEABLE) > 0); } bool page_desc_is_cow(page_desc_t pte) { return ((pte & PAGE_DESC_COPY_ON_WRITE) > 0); } uint32_t page_desc_get_frame(page_desc_t pte) { return ((pte >> PAGE_DESC_FRAME_OFFSET) << PAGE_DESC_FRAME_OFFSET); } uint32_t page_desc_get_settings(page_desc_t pte) { uint32_t res = MMU_FLAG_PERM_READ; if (page_desc_is_writable(pte)) { res |= MMU_FLAG_PERM_WRITE; } if (page_desc_is_user(pte)) { res |= MMU_FLAG_NONPRIV; } if (page_desc_is_not_cacheable(pte)) { res |= MMU_FLAG_UNCACHED; } if (page_desc_is_cow(pte)) { res |= MMU_FLAG_COW; } return res; } uint32_t page_desc_get_settings_ignore_cow(page_desc_t pte) { uint32_t res = MMU_FLAG_PERM_READ; if (page_desc_is_writable(pte)) { res |= MMU_FLAG_PERM_WRITE; } if (page_desc_is_user(pte)) { res |= MMU_FLAG_NONPRIV; } if (page_desc_is_not_cacheable(pte)) { res |= MMU_FLAG_UNCACHED; } return res; }
willmexe/opuntiaOS
kernel/include/mem/bits/vm.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_MEM_BITS_VM_H #define _KERNEL_MEM_BITS_VM_H #include <platform/generic/vmm/consts.h> #include <platform/generic/vmm/pde.h> #include <platform/generic/vmm/pte.h> #define pdir_t pdirectory_t #define VMM_TOTAL_PAGES_PER_TABLE VMM_PTE_COUNT #define VMM_TOTAL_TABLES_PER_DIRECTORY VMM_PDE_COUNT #define PDIR_SIZE sizeof(pdirectory_t) #define PTABLE_SIZE sizeof(ptable_t) #define IS_INDIVIDUAL_PER_DIR(index) (index < VMM_KERNEL_TABLES_START || (index == VMM_OFFSET_IN_DIRECTORY(pspace_zone.start))) enum PTABLE_LEVELS { PTABLE_LV0 = 1, PTABLE_LV1 = 2, PTABLE_LV2 = 3, PTABLE_LV3 = 4, }; typedef enum PTABLE_LEVELS ptable_lv_t; typedef struct { page_desc_t entities[VMM_PTE_COUNT]; } ptable_t; typedef struct pdirectory { table_desc_t entities[VMM_PDE_COUNT]; } pdirectory_t; #endif // _KERNEL_MEM_BITS_VM_H
willmexe/opuntiaOS
libs/libfoundation/include/libfoundation/json/Lexer.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include <cctype> #include <string> #include <unistd.h> namespace LFoundation::Json { class Lexer { public: Lexer() = default; Lexer(const std::string& filepath) : m_text_data() , m_pointer(0) { set_file(filepath); } ~Lexer() = default; int set_file(const std::string& filepath); bool is_eof() const { return m_pointer >= m_text_data.size(); } int lookup_char() const { if (is_eof()) { return EOF; } return m_text_data[m_pointer]; } int next_char() { if (is_eof()) { return EOF; } return m_text_data[m_pointer++]; } void skip_spaces() { while (std::isspace(lookup_char())) { next_char(); } } bool eat_string(std::string& word) { // TODO: Fix stop at " while (std::isprint(lookup_char()) && lookup_char() != '\"') { word.push_back(next_char()); }; return true; } bool eat_token(char what) { skip_spaces(); return next_char() == what; } private: int m_pointer; std::string m_text_data; }; } // namespace LFoundation
willmexe/opuntiaOS
servers/window_server/src/Managers/InitManager.h
<reponame>willmexe/opuntiaOS /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include "../Components/ControlBar/ControlBar.h" #include "../Components/LoadingScreen/LoadingScreen.h" #include "../Components/MenuBar/MenuBar.h" #include "../Components/MenuBar/Widgets/Clock/Clock.h" #include "../Components/MenuBar/Widgets/ControlPanelToggle/ControlPanelToggle.h" #include "../Components/Popup/Popup.h" #include "../Devices/Devices.h" #include "../Devices/Screen.h" #include "../IPC/Connection.h" #include "Compositor.h" #include "CursorManager.h" #include "ResourceManager.h" #include "WindowManager.h" #include <cstdlib> #include <libfoundation/EventLoop.h> #include <new> #include <sys/socket.h> #include <unistd.h> namespace WinServer { class InitManager { public: InitManager() = delete; ~InitManager() = delete; static void load_screen() { nice(-3); new WinServer::Screen(); new WinServer::LoadingScreen(); } template <class T, int Cost = 1, class... Args> static constexpr inline void load_core_component(Args&&... args) { new T(std::forward<Args>(args)...); WinServer::LoadingScreen::the().move_progress<T, Cost>(); } template <class T, int Cost = 1, class... Args> static constexpr inline void add_widget(Args&&... args) { WinServer::MenuBar::the().add_widget<T>(std::forward<Args>(args)...); WinServer::LoadingScreen::the().move_progress<T, Cost>(); } static inline int launch_app(const char* path) { int pid = fork(); if (!pid) { for (int i = 3; i < 32; i++) { close(i); } execlp(path, path, NULL); std::abort(); } return pid; } private: }; };
willmexe/opuntiaOS
userland/system/applist/AppListView.h
#pragma once #include "DockEntity.h" #include "IconView.h" #include "WindowEntity.h" #include <libg/Font.h> #include <libui/StackView.h> #include <libui/View.h> #include <list> #include <string> class AppListView : public UI::View { UI_OBJECT(); public: AppListView(UI::View* superview, const LG::Rect& frame); AppListView(UI::View* superview, UI::Window* window, const LG::Rect& frame); static constexpr size_t padding() { return 0; } static constexpr size_t dock_view_height() { return 46; } static constexpr int icon_size() { return 48; } static constexpr int icon_view_size() { return (int)60; } void display(const LG::Rect& rect) override; void new_dock_entity(const std::string& exec_path, const std::string& icon_path, const std::string& bundle_id); private: void launch(const DockEntity& ent); UI::StackView* m_dock_stackview {}; std::list<IconView*> m_icon_views {}; };
willmexe/opuntiaOS
kernel/kernel/platform/x86/tasking/trapframe.c
<gh_stars>100-1000 #include <platform/x86/tasking/trapframe.h>
willmexe/opuntiaOS
kernel/include/drivers/x86/ata.h
<gh_stars>100-1000 /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_DRIVERS_X86_ATA_H #define _KERNEL_DRIVERS_X86_ATA_H #include <drivers/driver_manager.h> #include <drivers/x86/display.h> #include <libkern/types.h> #include <mem/kmalloc.h> #include <platform/x86/port.h> typedef struct { // LBA28 | LBA48 uint32_t data; // 16bit | 16 bits uint32_t error; // 8 bit | 16 bits uint32_t sector_count; // 8 bit | 16 bits uint32_t lba_lo; // 8 bit | 16 bits uint32_t lba_mid; // 8 bit | 16 bits uint32_t lba_hi; // 8 bit | 16 bits uint32_t device; // 8 bit uint32_t command; // 8 bit uint32_t control; } ata_ports_t; typedef struct { ata_ports_t port; bool is_master; uint16_t cylindres; uint16_t heads; uint16_t sectors; bool dma; bool lba; uint32_t capacity; // in sectors } ata_t; extern ata_t _ata_drives[MAX_DEVICES_COUNT]; int ata_init_with_dev(device_t* dev); void ata_install(); void ata_init(ata_t* ata, uint32_t port, bool is_master); bool ata_indentify(ata_t* ata); #endif //_KERNEL_DRIVERS_X86_ATA_H
willmexe/opuntiaOS
libs/libc/include/bits/thread.h
#ifndef _LIBC_BITS_THREAD_H #define _LIBC_BITS_THREAD_H #include <sys/types.h> struct thread_create_params { uint32_t entry_point; uint32_t stack_start; uint32_t stack_size; }; typedef struct thread_create_params thread_create_params_t; #endif // _LIBC_BITS_THREAD_H
willmexe/opuntiaOS
test/kernel/env/main.c
#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char** argv) { if (getenv("OSTEST")) { char* res = getenv("GO"); if (!res) { TestErr("Empty GO env var."); } if (strncmp(res, "go", 2)) { TestErr("Different value in GO env var."); } return 0; } if (argc > 1) { TestErr("Loop while executing test."); } setenv("OSTEST", "1", 1); setenv("GO", "go", 1); execlp(argv[0], argv[0], "loop", NULL); return 1; }
willmexe/opuntiaOS
kernel/include/algo/hash.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_ALGO_HASH_H #define _KERNEL_ALGO_HASH_H #include <libkern/types.h> #define hashint(hfunc, val) (hfunc((uint8_t*)&val, sizeof(val))) uint32_t hash_crc32(uint8_t* data, size_t len); uint32_t hashstr_crc32(char* data); #endif // _KERNEL_ALGO_HASH_H
willmexe/opuntiaOS
libs/libc/init/_init.c
<filename>libs/libc/init/_init.c extern void _libc_init(int argc, char* argv[], char* envp[]); extern void _libc_deinit(); void _init(int argc, char* argv[], char* envp[]) { _libc_init(argc, argv, envp); } void _deinit() { _libc_deinit(); }
willmexe/opuntiaOS
kernel/include/libkern/bits/sys/ptrace.h
#ifndef _KERNEL_LIBKERN_BITS_SYS_PTRACE_H #define _KERNEL_LIBKERN_BITS_SYS_PTRACE_H #include <libkern/types.h> typedef int ptrace_request_t; #define PTRACE_TRACEME (0x1) #define PTRACE_CONT (0x2) #define PTRACE_PEEKTEXT (0x3) #define PTRACE_PEEKDATA (0x4) #endif // _KERNEL_LIBKERN_BITS_SYS_PTRACE_H
willmexe/opuntiaOS
kernel/include/platform/aarch32/tasking/context.h
<gh_stars>100-1000 /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_PLATFORM_AARCH32_TASKING_CONTEXT_H #define _KERNEL_PLATFORM_AARCH32_TASKING_CONTEXT_H #include <libkern/c_attrs.h> #include <libkern/types.h> typedef struct { uint32_t r[9]; uint32_t lr; } PACKED context_t; static inline uintptr_t context_get_instruction_pointer(context_t* ctx) { return ctx->lr; } static inline void context_set_instruction_pointer(context_t* ctx, uintptr_t ip) { ctx->lr = ip; } #endif // _KERNEL_PLATFORM_AARCH32_TASKING_CONTEXT_H
willmexe/opuntiaOS
kernel/kernel/platform/x86/tasking/dump_impl.c
<reponame>willmexe/opuntiaOS<filename>kernel/kernel/platform/x86/tasking/dump_impl.c /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <libkern/log.h> #include <mem/vmm.h> #include <platform/x86/registers.h> #include <platform/x86/system.h> #include <platform/x86/tasking/dump_impl.h> void dump_regs(dump_data_t* dump_data) { // 3 rows of: 3chars (reg name) + 2chars (separartor) + 12chars (reg value) char buf[64]; trapframe_t* tf = dump_data->p->main_thread->tf; snprintf(buf, 64, "EAX: %x EBX: %x ECX: %x\n", tf->eax, tf->ebx, tf->ecx); dump_data->writer(buf); snprintf(buf, 64, "EDX: %x ESI: %x EDI: %x\n", tf->edx, tf->esi, tf->edi); dump_data->writer(buf); snprintf(buf, 64, "EIP: %x ESP: %x EBP: %x\n\n", tf->eip, tf->esp, tf->ebp); dump_data->writer(buf); } void dump_backtrace(dump_data_t* dump_data, uintptr_t ip, uintptr_t* bp, int is_kernel) { uint32_t id = 1; char buf[64]; do { if (vmm_is_kernel_address(ip) && !is_kernel) { return; } snprintf(buf, 64, "[%d] %x : ", id, ip); dump_data->writer(buf); int index = dump_data->sym_resolver(dump_data->syms, dump_data->symsn, ip); dump_data->writer(&dump_data->strs[index]); dump_data->writer("\n"); if (vmm_is_kernel_address((uintptr_t)bp) != is_kernel) { return; } ip = bp[1]; bp = (uintptr_t*)*bp; id++; } while (ip != dump_data->entry_point); return; } int dump_impl(dump_data_t* dump_data) { trapframe_t* tf = dump_data->p->main_thread->tf; dump_regs(dump_data); dump_backtrace(dump_data, get_instruction_pointer(tf), (uint32_t*)get_base_pointer(tf), 0); return 0; } int dump_kernel_impl(dump_data_t* dump_data, const char* err_desc) { if (err_desc) { dump_data->writer(err_desc); } dump_backtrace(dump_data, read_ip(), (uint32_t*)read_bp(), 1); return 0; } int dump_kernel_impl_from_tf(dump_data_t* dump_data, const char* err_desc, trapframe_t* tf) { if (err_desc) { dump_data->writer(err_desc); } dump_backtrace(dump_data, tf->eip, (uint32_t*)tf->ebp, 1); return 0; }
willmexe/opuntiaOS
kernel/kernel/syscalls/ptrace.c
<reponame>willmexe/opuntiaOS /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <libkern/bits/errno.h> #include <libkern/bits/sys/ptrace.h> #include <libkern/libkern.h> #include <libkern/log.h> #include <platform/generic/syscalls/params.h> #include <syscalls/handlers.h> #include <tasking/tasking.h> void sys_ptrace(trapframe_t* tf) { ptrace_request_t request = SYSCALL_VAR1(tf); if (request == PTRACE_TRACEME) { RUNNING_THREAD->process->is_tracee = true; return_with_val(0); } thread_t* thread = thread_by_pid(SYSCALL_VAR2(tf)); if (!thread) { return_with_val(-ESRCH); } switch (request) { case PTRACE_CONT: tasking_signal(thread, SIGCONT); break; default: return_with_val(-EINVAL); } return_with_val(0); }
willmexe/opuntiaOS
libs/libfoundation/include/libfoundation/json/Object.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include <cassert> #include <cstddef> #include <map> #include <string> #include <vector> namespace LFoundation::Json { class Object { public: enum Type { Array, String, Number, Dict, Invalid, }; constexpr static Object::Type ObjType = Type::Invalid; Object() = default; Object(Type type) : m_type(type) { } ~Object() = default; Type type() const { return m_type; } bool invalid() const { return type() == Type::Invalid; } template <class ObjectT> Object* assert_object() { assert(ObjectT::ObjType == m_type); return this; } template <class ObjectT> ObjectT* cast_to_no_assert() { return (ObjectT*)this; } template <class ObjectT> ObjectT* cast_to() { return (ObjectT*)assert_object<ObjectT>(); } private: Type m_type { Invalid }; }; class StringObject : public Object { public: constexpr static Object::Type ObjType = Type::String; StringObject() : Object(ObjType) { } ~StringObject() = default; std::string& data() { return m_data; } const std::string& data() const { return m_data; } private: std::string m_data; }; class DictObject : public Object { public: constexpr static Object::Type ObjType = Type::Dict; DictObject() : Object(ObjType) { } ~DictObject() { // TODO: Add iterators to map. // for (auto it : m_data) { // delete it.second; // } } std::map<std::string, Object*>& data() { return m_data; } const std::map<std::string, Object*>& data() const { return m_data; } private: std::map<std::string, Object*> m_data; }; class InvalidObject : public Object { public: constexpr static Object::Type ObjType = Type::Invalid; InvalidObject() : Object(ObjType) { } ~InvalidObject() = default; }; } // namespace LFoundation
willmexe/opuntiaOS
libs/libc/include/time.h
<filename>libs/libc/include/time.h<gh_stars>100-1000 #ifndef _LIBC_TIME_H #define _LIBC_TIME_H #include <bits/time.h> #include <fcntl.h> #include <stddef.h> #include <sys/cdefs.h> #include <sys/types.h> __BEGIN_DECLS time_t time(time_t* timep); char* asctime(const tm_t* tm); char* asctime_r(const tm_t* tm, char* buf); char* ctime(const time_t* timep); char* ctime_r(const time_t* timep, char* buf); tm_t* gmtime(const time_t* timep); tm_t* gmtime_r(const time_t* timep, tm_t* result); tm_t* localtime(const time_t* timep); tm_t* localtime_r(const time_t* timep, tm_t* result); size_t strftime(char* s, size_t max, const char* format, const tm_t* tm); time_t mktime(tm_t* tm); int clock_getres(clockid_t clk_id, timespec_t* res); int clock_gettime(clockid_t clk_id, timespec_t* tp); int clock_settime(clockid_t clk_id, const timespec_t* tp); __END_DECLS #endif // _LIBC_TIME_H
willmexe/opuntiaOS
kernel/include/platform/aarch32/target/cortex-a15/device_settings.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_PLATFORM_AARCH32_TARGET_CORTEX_A15_DEVICE_SETTINGS_H #define _KERNEL_PLATFORM_AARCH32_TARGET_CORTEX_A15_DEVICE_SETTINGS_H /** * Used devices: * uart * gicv2 * sp804 * pl181 * pl111 * pl050 * pl031 */ /* Base is read from CBAR */ #define GICv2_DISTRIBUTOR_OFFSET 0x1000 #define GICv2_CPU_INTERFACE_OFFSET 0x2000 #define UART_BASE 0x1c090000 #define SP804_BASE 0x1c110000 #define PL181_BASE 0x1c050000 #define PL111_BASE 0x1c1f0000 #define PL050_KEYBOARD_BASE 0x1c060000 #define PL050_MOUSE_BASE 0x1c070000 #define PL031_BASE 0x1c170000 /** * Interrupt lines: * SP804 TIMER1: 2nd line in SPI (32+2) */ #define SP804_TIMER1_IRQ_LINE (32 + 2) #define PL050_KEYBOARD_IRQ_LINE (32 + 12) #define PL050_MOUSE_IRQ_LINE (32 + 13) #endif /* _KERNEL_PLATFORM_AARCH32_TARGET_CORTEX_A15_DEVICE_SETTINGS_H */
willmexe/opuntiaOS
test/kernel/fs/dirfile/main.c
<filename>test/kernel/fs/dirfile/main.c<gh_stars>100-1000 #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> char buf[512]; int main(int argc, char** argv) { int fd; fd = open("dirfile", 0); if (fd >= 0) { TestErr("create dirfile succeeded"); } fd = open("dirfile", O_CREAT); if (chdir("dirfile") == 0) { TestErr("chdir dirfile succeeded"); } if (unlink("dirfile") != 0) { TestErr("unlink dirfile succeeded"); } fd = open(".", O_RDWR); if (fd >= 0) { TestErr("open . for writing succeeded"); } fd = open(".", 0); if (write(fd, "x", 1) > 0) { TestErr("write . succeeded"); } close(fd); return 0; }
willmexe/opuntiaOS
boot/libboot/mem/mem.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _BOOT_LIBBOOT_MEM_MEM_H #define _BOOT_LIBBOOT_MEM_MEM_H #include <libboot/types.h> int strcmp(const char* a, const char* b); int strncmp(const char* a, const char* b, uint32_t num); size_t strlen(const char* s); void* memset(void* dest, uint8_t fll, uint32_t nbytes); void* memcpy(void* dest, const void* src, uint32_t nbytes); void* memccpy(void* dest, const void* src, uint8_t stop, uint32_t nbytes); void* memmove(void* dest, const void* src, uint32_t nbytes); int memcmp(const void* src1, const void* src2, uint32_t nbytes); static size_t align_size(size_t size, size_t align) { if (size % align) { size += align - (size % align); } return size; } static inline void* copy_after_kernel(size_t kbase, void* from, size_t size, size_t* kernel_size, size_t align) { void* pp = (void*)(kbase + *kernel_size); memcpy(pp, from, size); *kernel_size += align_size(size, align); return pp; } static inline void* paddr_to_vaddr(void* ptr, size_t pbase, size_t vbase) { return (void*)((size_t)ptr - pbase + vbase); } #endif // _BOOT_LIBBOOT_MEM_MEM_H
willmexe/opuntiaOS
kernel/include/platform/aarch32/pmm/settings.h
<reponame>willmexe/opuntiaOS /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_PLATFORM_AARCH32_PMM_SETTINGS_H #define _KERNEL_PLATFORM_AARCH32_PMM_SETTINGS_H #define KERNEL_PM_BASE 0x80100000 #define KERNEL_BASE 0xc0000000 #define KMALLOC_BASE (KERNEL_BASE + 0x400000) #define PMM_BLOCK_SIZE (1024) #define PMM_BLOCK_SIZE_KB (1) #define PMM_BLOCKS_PER_BYTE (8) #endif /* _KERNEL_PLATFORM_AARCH32_PMM_SETTINGS_H */
willmexe/opuntiaOS
libs/libc/include/bits/types.h
<gh_stars>100-1000 #ifndef _LIBC_BITS_TYPES_H #define _LIBC_BITS_TYPES_H typedef char __int8_t; typedef short __int16_t; typedef int __int32_t; typedef long long __int64_t; typedef unsigned char __uint8_t; typedef unsigned short __uint16_t; typedef unsigned int __uint32_t; typedef unsigned long long __uint64_t; typedef __uint32_t __dev_t; /* Type of device numbers. */ typedef __uint32_t __uid_t; /* Type of user identifications. */ typedef __uint32_t __gid_t; /* Type of group identifications. */ typedef __uint32_t __ino_t; /* Type of file serial numbers. */ typedef __uint64_t __ino64_t; /* Type of file serial numbers (LFS).*/ typedef __uint16_t __mode_t; /* Type of file attribute bitmasks. */ typedef __uint32_t __nlink_t; /* Type of file link counts. */ typedef __int32_t __off_t; /* Type of file sizes and offsets. */ typedef __int64_t __off64_t; /* Type of file sizes and offsets (LFS). */ typedef __uint32_t __pid_t; /* Type of process identifications. */ typedef __uint32_t __fsid_t; /* Type of file system IDs. */ typedef __uint32_t __time_t; /* Seconds since the Epoch. */ #endif // _LIBC_BITS_TYPES_H
willmexe/opuntiaOS
servers/window_server/src/Target/Desktop/Window.h
<filename>servers/window_server/src/Target/Desktop/Window.h /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include "../../Components/Base/BaseWindow.h" #include "../../Components/MenuBar/MenuItem.h" #include "../../IPC/Connection.h" #include "WindowFrame.h" #include <libfoundation/SharedBuffer.h> #include <libg/PixelBitmap.h> #include <libg/Rect.h> #include <sys/types.h> #include <utility> namespace WinServer::Desktop { class Window : public BaseWindow { public: Window(int connection_id, int id, CreateWindowMessage& msg); Window(Window&& win); inline WindowFrame& frame() { return m_frame; } inline const WindowFrame& frame() const { return m_frame; } inline const LG::CornerMask& corner_mask() const { return m_corner_mask; } inline std::vector<MenuDir>& menubar_content() { return m_menubar_content; } inline const std::vector<MenuDir>& menubar_content() const { return m_menubar_content; } void on_menubar_change(); virtual void did_app_title_change() override { m_frame.on_set_app_title(); } virtual void did_icon_path_change() override { m_frame.on_set_icon(); } virtual void did_size_change(const LG::Size& size) override; inline void set_style(StatusBarStyle style) { m_frame.set_style(style), on_style_change(); } void make_frame(); void make_frameless(); private: void recalc_bounds(const LG::Size& size); void on_style_change(); WindowFrame m_frame; LG::CornerMask m_corner_mask { LG::CornerMask::SystemRadius, LG::CornerMask::NonMasked, LG::CornerMask::Masked }; std::vector<MenuDir> m_menubar_content; }; } // namespace WinServer
willmexe/opuntiaOS
kernel/kernel/syscalls/io.c
<reponame>willmexe/opuntiaOS /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <io/shared_buffer/shared_buffer.h> #include <io/sockets/local_socket.h> #include <libkern/bits/errno.h> #include <libkern/libkern.h> #include <libkern/log.h> #include <platform/generic/syscalls/params.h> #include <syscalls/handlers.h> #include <tasking/tasking.h> void sys_socket(trapframe_t* tf) { proc_t* p = RUNNING_THREAD->process; int domain = SYSCALL_VAR1(tf); int type = SYSCALL_VAR2(tf); int protocol = SYSCALL_VAR3(tf); file_descriptor_t* fd = proc_get_free_fd(p); if (!fd) { return_with_val(-1); } if (domain == PF_LOCAL) { int res = local_socket_create(type, protocol, fd); if (!res) { return_with_val(proc_get_fd_id(p, fd)); } return_with_val(res); } return_with_val(-1); } void sys_bind(trapframe_t* tf) { proc_t* p = RUNNING_THREAD->process; int sockfd = SYSCALL_VAR1(tf); char* name = (char*)SYSCALL_VAR2(tf); uint32_t len = (uint32_t)SYSCALL_VAR3(tf); file_descriptor_t* sfd = proc_get_fd(p, sockfd); if (sfd->type != FD_TYPE_SOCKET || !sfd->sock_entry) { return_with_val(-EBADF); } if (sfd->sock_entry->domain == PF_LOCAL) { return_with_val(local_socket_bind(sfd, name, len)); } return_with_val(0); } void sys_connect(trapframe_t* tf) { proc_t* p = RUNNING_THREAD->process; int sockfd = SYSCALL_VAR1(tf); char* name = (char*)SYSCALL_VAR2(tf); uint32_t len = (uint32_t)SYSCALL_VAR3(tf); file_descriptor_t* sfd = proc_get_fd(p, sockfd); if (sfd->type != FD_TYPE_SOCKET || !sfd->sock_entry) { return_with_val(-EBADF); } if (sfd->sock_entry->domain == PF_LOCAL) { return_with_val(local_socket_connect(sfd, name, len)); } return_with_val(-EFAULT); } void sys_ioctl(trapframe_t* tf) { proc_t* p = RUNNING_THREAD->process; file_descriptor_t* fd = proc_get_fd(p, SYSCALL_VAR1(tf)); if (!fd) { return_with_val(-EBADF); } if (!fd->dentry->ops->file.ioctl) { return_with_val(-EACCES); } return_with_val(fd->dentry->ops->file.ioctl(fd->dentry, SYSCALL_VAR2(tf), SYSCALL_VAR3(tf))); } void sys_shbuf_create(trapframe_t* tf) { uint8_t** buffer = (uint8_t**)SYSCALL_VAR1(tf); size_t size = SYSCALL_VAR2(tf); return_with_val(shared_buffer_create(buffer, size)); } void sys_shbuf_get(trapframe_t* tf) { int id = SYSCALL_VAR1(tf); uint8_t** buffer = (uint8_t**)SYSCALL_VAR2(tf); return_with_val(shared_buffer_get(id, buffer)); } void sys_shbuf_free(trapframe_t* tf) { int id = SYSCALL_VAR1(tf); return_with_val(shared_buffer_free(id)); }
willmexe/opuntiaOS
kernel/kernel/mem/vm_alloc.c
<filename>kernel/kernel/mem/vm_alloc.c<gh_stars>0 /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <mem/pmm.h> #include <mem/vm_alloc.h> #include <mem/vm_pspace.h> #include <mem/vmm.h> uintptr_t vm_alloc_pdir_paddr() { return (uintptr_t)pmm_alloc_aligned(PDIR_SIZE, PDIR_SIZE); } uintptr_t vm_alloc_ptable_paddr() { return (uintptr_t)pmm_alloc_aligned(PTABLE_SIZE, PTABLE_SIZE); } uintptr_t vm_alloc_ptables_to_cover_page() { return (uintptr_t)pmm_alloc_aligned(VMM_PAGE_SIZE, VMM_PAGE_SIZE); } void vm_free_ptables_to_cover_page(uintptr_t addr) { pmm_free((void*)addr, VMM_PAGE_SIZE); } uintptr_t vm_alloc_page_paddr() { return (uintptr_t)pmm_alloc_aligned(VMM_PAGE_SIZE, VMM_PAGE_SIZE); } void vm_free_page_paddr(uintptr_t addr) { pmm_free((void*)addr, VMM_PAGE_SIZE); } kmemzone_t vm_alloc_mapped_zone(size_t size, size_t alignment) { if (size % VMM_PAGE_SIZE) { size += VMM_PAGE_SIZE - (size % VMM_PAGE_SIZE); } if (alignment % VMM_PAGE_SIZE) { alignment += VMM_PAGE_SIZE - (alignment % VMM_PAGE_SIZE); } // TODO: Currently only sequence allocation is implemented. kmemzone_t zone = kmemzone_new_aligned(size, alignment); uintptr_t paddr = (uintptr_t)pmm_alloc_aligned(size, alignment); vmm_map_pages_lockless(zone.start, paddr, size / VMM_PAGE_SIZE, MMU_FLAG_PERM_READ | MMU_FLAG_PERM_WRITE); return zone; } int vm_free_mapped_zone(kmemzone_t zone) { page_desc_t* page = vm_pspace_get_page_desc(zone.start); pmm_free((void*)page_desc_get_frame(*page), zone.len); vmm_unmap_pages_lockless(zone.start, zone.len / VMM_PAGE_SIZE); kmemzone_free(zone); return 0; } pdirectory_t* vm_alloc_pdir() { kmemzone_t zone = vm_alloc_mapped_zone(PDIR_SIZE, PDIR_SIZE); return (pdirectory_t*)zone.ptr; } void vm_free_pdir(pdirectory_t* pdir) { if (pdir == vmm_get_kernel_pdir()) { return; } kmemzone_t zone; zone.start = (uintptr_t)pdir; zone.len = PDIR_SIZE; vm_free_mapped_zone(zone); }
willmexe/opuntiaOS
boot/aarch32/drivers/uart.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _BOOT_DRIVERS_UART_H #define _BOOT_DRIVERS_UART_H #include <libboot/types.h> void uart_init(); int uart_write(uint8_t data); #endif // _BOOT_DRIVERS_UART_H
willmexe/opuntiaOS
libs/libobjc/include/libobjc/helpers.h
<gh_stars>100-1000 /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _LIBOBJC_HELPERS_H #define _LIBOBJC_HELPERS_H #include <stdio.h> #define OBJC_EXPORT extern "C" #define OBJC_DEBUG #ifdef OBJC_DEBUG #define OBJC_DEBUGPRINT(...) \ printf(__VA_ARGS__); \ fflush(stdout) #else #define OBJC_DEBUGPRINT(...) (sizeof(int)) #endif #endif // _LIBOBJC_HELPERS_H
willmexe/opuntiaOS
libs/libc/posix/fs.c
<filename>libs/libc/posix/fs.c #include <fcntl.h> #include <sys/mman.h> #include <sys/select.h> #include <sys/stat.h> #include <sysdep.h> #include <unistd.h> int open(const char* pathname, int flags) { int res = DO_SYSCALL_3(SYS_OPEN, pathname, flags, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO); RETURN_WITH_ERRNO(res, res, -1); } int creat(const char* path, mode_t mode) { int res = DO_SYSCALL_2(SYS_CREAT, path, mode); RETURN_WITH_ERRNO(res, res, -1); } int close(int fd) { int res = DO_SYSCALL_1(SYS_CLOSE, fd); RETURN_WITH_ERRNO(res, 0, -1); } ssize_t read(int fd, char* buf, size_t count) { return (ssize_t)DO_SYSCALL_3(SYS_READ, fd, buf, count); } ssize_t write(int fd, const void* buf, size_t count) { return (ssize_t)DO_SYSCALL_3(SYS_WRITE, fd, buf, count); } int dup(int oldfd) { int res = DO_SYSCALL_1(SYS_DUP, oldfd); RETURN_WITH_ERRNO(res, res, -1); } int dup2(int oldfd, int newfd) { int res = DO_SYSCALL_2(SYS_DUP2, oldfd, newfd); RETURN_WITH_ERRNO(res, res, -1); } off_t lseek(int fd, off_t off, int whence) { return (off_t)DO_SYSCALL_3(SYS_LSEEK, fd, off, whence); } int mkdir(const char* path) { int res = DO_SYSCALL_1(SYS_MKDIR, path); RETURN_WITH_ERRNO(res, 0, -1); } int rmdir(const char* path) { int res = DO_SYSCALL_1(SYS_RMDIR, path); RETURN_WITH_ERRNO(res, 0, -1); } int chdir(const char* path) { int res = DO_SYSCALL_1(SYS_CHDIR, path); RETURN_WITH_ERRNO(res, 0, -1); } char* getcwd(char* buf, size_t size) { int res = DO_SYSCALL_2(SYS_GETCWD, buf, size); RETURN_WITH_ERRNO(res, buf, NULL); } int unlink(const char* path) { int res = DO_SYSCALL_1(SYS_UNLINK, path); RETURN_WITH_ERRNO(res, 0, -1); } int fstat(int nfds, fstat_t* stat) { int res = DO_SYSCALL_2(SYS_FSTAT, nfds, stat); RETURN_WITH_ERRNO(res, 0, -1); } int select(int nfds, fd_set_t* readfds, fd_set_t* writefds, fd_set_t* exceptfds, timeval_t* timeout) { int res = DO_SYSCALL_5(SYS_SELECT, nfds, readfds, writefds, exceptfds, timeout); RETURN_WITH_ERRNO(res, res, -1); } void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) { mmap_params_t mmap_params = { 0 }; mmap_params.addr = addr; mmap_params.size = length; mmap_params.prot = prot; mmap_params.flags = flags; mmap_params.fd = fd; mmap_params.offset = offset; return (void*)DO_SYSCALL_1(SYS_MMAP, &mmap_params); } int munmap(void* addr, size_t length) { int res = DO_SYSCALL_2(SYS_MUNMAP, addr, length); RETURN_WITH_ERRNO(res, 0, -1); }
willmexe/opuntiaOS
boot/aarch32/drivers/pl181.c
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include "pl181.h" #include <libboot/devtree/devtree.h> #include <libboot/log/log.h> // #define DEBUG_PL181 static sd_card_t sd_card; static volatile pl181_registers_t* registers; static inline int _pl181_map_itself(devtree_entry_t* dev) { registers = (pl181_registers_t*)dev->paddr; return 0; } static inline void _pl181_clear_flags() { registers->clear = 0x5FF; } static int _pl181_send_cmd(uint32_t cmd, uint32_t param) { _pl181_clear_flags(); registers->arg = param; registers->cmd = cmd; while (registers->status & MMC_STAT_CMD_ACTIVE_MASK) { } if ((cmd & MMC_CMD_RESP_MASK) == MMC_CMD_RESP_MASK) { while (((registers->status & MMC_STAT_CMD_RESP_END_MASK) != MMC_STAT_CMD_RESP_END_MASK) || (registers->status & MMC_STAT_CMD_ACTIVE_MASK)) { if (registers->status & MMC_STAT_CMD_TIMEOUT_MASK) { return -1; } } } else { while ((registers->status & MMC_STAT_CMD_SENT_MASK) != MMC_STAT_CMD_SENT_MASK) { } } return 0; } static int _pl181_send_app_cmd(uint32_t cmd, uint32_t param) { for (int i = 0; i < 5; i++) { _pl181_send_cmd(CMD_APP_CMD | MMC_CMD_ENABLE_MASK | MMC_CMD_RESP_MASK, 0); if (registers->response[0] & (1 << 5)) { return _pl181_send_cmd(cmd, param); } } log_error("Failed to send app command"); return -4; } inline static int _pl181_select_card(uint32_t rca) { return _pl181_send_cmd(CMD_SELECT | MMC_CMD_ENABLE_MASK | MMC_CMD_RESP_MASK, rca); } static int _pl181_read_block(uint32_t lba_like, void* read_data) { sd_card_t* sd_card_ptr = &sd_card; uint32_t* read_data32 = (uint32_t*)read_data; uint32_t bytes_read = 0; registers->data_length = PL181_SECTOR_SIZE; // Set length of bytes to transfer registers->data_control = 0b11; // Enable dpsm and set direction from card to host if (sd_card_ptr->ishc) { _pl181_send_cmd(CMD_READ_SINGLE_BLOCK | MMC_CMD_ENABLE_MASK | MMC_CMD_RESP_MASK, lba_like); } else { _pl181_send_cmd(CMD_READ_SINGLE_BLOCK | MMC_CMD_ENABLE_MASK | MMC_CMD_RESP_MASK, lba_like * PL181_SECTOR_SIZE); } while (registers->status & MMC_STAT_FIFO_DATA_AVAIL_TO_READ_MASK) { *read_data32 = registers->fifo_data[0]; read_data32++; bytes_read += 4; } return bytes_read; } static int _pl181_register_device(uint32_t rca, bool ishc, uint32_t capacity) { sd_card.rca = rca; sd_card.ishc = ishc; sd_card.capacity = capacity; _pl181_select_card(rca); _pl181_send_cmd(CMD_SET_SECTOR_SIZE | MMC_CMD_ENABLE_MASK | MMC_CMD_RESP_MASK, PL181_SECTOR_SIZE); if (registers->response[0] != 0x900) { log_error("PL181(pl181_add_new_device): Can't set sector size"); return -1; } return 0; } int pl181_init(drive_desc_t* drive_desc) { devtree_entry_t* dev = devtree_find_device("pl181"); if (!dev) { return -1; } if (_pl181_map_itself(dev)) { #ifdef DEBUG_PL181 log_error("PL181: Can't map itself!"); #endif return -1; } #ifdef DEBUG_PL181 log("PL181: Turning on"); #endif registers->clock = 0x1C6; registers->power = 0x86; // Send cmd 0 to set all cards into idle state _pl181_send_cmd(CMD_GO_IDLE_STATE | MMC_CMD_ENABLE_MASK, 0x0); // Voltage Check _pl181_send_cmd(8 | MMC_CMD_ENABLE_MASK | MMC_CMD_RESP_MASK, 0x1AA); if ((registers->response[0] & 0x1AA) != 0x1AA) { #ifdef DEBUG_PL181 log_error("PL181: Can't set voltage!"); #endif return -1; } if (_pl181_send_app_cmd(CMD_SD_SEND_OP_COND | MMC_CMD_ENABLE_MASK | MMC_CMD_RESP_MASK, 1 << 30 | 0x1AA)) { #ifdef DEBUG_PL181 log_error("PL181: Can't send APP_CMD!"); #endif return -1; } bool ishc = 0; if (registers->response[0] & 1 << 30) { ishc = 1; #ifdef DEBUG_PL181 log("PL181: ishc = 1"); #endif } _pl181_send_cmd(CMD_ALL_SEND_CID | MMC_CMD_ENABLE_MASK | MMC_CMD_LONG_RESP_MASK, 0x0); _pl181_send_cmd(CMD_SET_RELATIVE_ADDR | MMC_CMD_ENABLE_MASK | MMC_CMD_RESP_MASK, 0x0); // Get the card RCA from the response it is the top 16 bits of the 32 bit response uint32_t rca = (registers->response[0] & 0xFFFF0000); _pl181_send_cmd(CMD_SEND_CSD | MMC_CMD_ENABLE_MASK | MMC_CMD_RESP_MASK | MMC_CMD_LONG_RESP_MASK, rca); uint32_t resp1 = registers->response[1]; uint32_t capacity = ((resp1 >> 8) & 0x3) << 10; capacity |= (resp1 & 0xFF) << 2; capacity = 256 * 1024 * (capacity + 1); _pl181_register_device(rca, ishc, capacity); drive_desc->read = _pl181_read_block; return 0; }
willmexe/opuntiaOS
userland/utilities/uname/main.c
<gh_stars>100-1000 #include <stdio.h> #include <string.h> #include <sys/utsname.h> #include <unistd.h> int main(int argc, char** argv) { int fd, i; utsname_t uts; int rc = uname(&uts); if (rc < 0) { return 1; } int flag_s = 0; int flag_n = 0; int flag_r = 0; int flag_m = 0; if (argc == 1) { flag_s = 1; } else { for (int i = 1; i < argc; ++i) { if (argv[i][0] == '-') { for (const char* o = &argv[i][1]; *o; ++o) { switch (*o) { case 's': flag_s = 1; break; case 'n': flag_n = 1; break; case 'r': flag_r = 1; break; case 'm': flag_m = 1; break; case 'a': flag_s = flag_n = flag_r = flag_m = 1; break; } } } } } if (!flag_s && !flag_n && !flag_r && !flag_m) { flag_s = 1; } if (flag_s) { printf("%s ", uts.sysname); } if (flag_n) { printf("%s ", uts.nodename); // write(1, uts.nodename, strlen(uts.nodename)); } if (flag_r) { printf("%s ", uts.release); // write(1, uts.release, strlen(uts.release)); } if (flag_m) { printf("%s ", uts.machine); // write(1, uts.machine, strlen(uts.machine)); } return 0; }
willmexe/opuntiaOS
kernel/include/mem/pmm.h
#ifndef _KERNEL_MEM_PMM_H #define _KERNEL_MEM_PMM_H #include <algo/bitmap.h> #include <libkern/types.h> #include <mem/boot.h> #include <platform/generic/pmm/settings.h> struct pmm_state { size_t kernel_va_base; size_t kernel_size; bitmap_t mat; boot_desc_t* boot_desc; size_t ram_size; size_t ram_offset; size_t max_blocks; size_t used_blocks; }; typedef struct pmm_state pmm_state_t; void pmm_setup(boot_desc_t* boot_desc); void* pmm_alloc(size_t size); void* pmm_alloc_aligned(size_t size, size_t alignment); int pmm_free(void* ptr, size_t size); size_t pmm_get_ram_size(); size_t pmm_get_max_blocks(); size_t pmm_get_used_blocks(); size_t pmm_get_free_blocks(); size_t pmm_get_block_size(); size_t pmm_get_ram_in_kb(); size_t pmm_get_free_space_in_kb(); const pmm_state_t* pmm_get_state(); #endif // _KERNEL_MEM_PMM_H
willmexe/opuntiaOS
kernel/include/drivers/x86/bga.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_DRIVERS_X86_BGA_H #define _KERNEL_DRIVERS_X86_BGA_H #include <drivers/driver_manager.h> #include <drivers/x86/display.h> #include <libkern/types.h> #include <mem/kmalloc.h> #include <platform/x86/port.h> void bga_install(); int bga_init_with_dev(device_t* dev); void bga_set_resolution(uint16_t width, uint16_t height); #endif //_KERNEL_DRIVERS_X86_BGA_H
willmexe/opuntiaOS
kernel/include/drivers/bits/device.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_DRIVERS_BITS_DEVICE_H #define _KERNEL_DRIVERS_BITS_DEVICE_H #include <libkern/types.h> enum DEVICES_TYPE { DEVICE_STORAGE = (1 << 0), DEVICE_SOUND = (1 << 1), DEVICE_INPUT_SYSTEMS = (1 << 2), DEVICE_NETWORK = (1 << 3), DEVICE_DISPLAY = (1 << 4), DEVICE_BUS_CONTROLLER = (1 << 5), DEVICE_BRIDGE = (1 << 6), DEVICE_CHAR = (1 << 7), DEVICE_UNKNOWN = (1 << 8), }; enum DEVICE_DESC_TYPE { DEVICE_DESC_PCI, DEVICE_DESC_DEVTREE, }; struct device_desc_pci { uint8_t bus; uint8_t device; uint8_t function; uint16_t vendor_id; uint16_t device_id; uint8_t class_id; uint8_t subclass_id; uint8_t interface_id; uint8_t revision_id; uint32_t interrupt; uint32_t port_base; }; typedef struct device_desc_pci device_desc_pci_t; struct devtree_entry; struct device_desc_devtree { struct devtree_entry* entry; }; typedef struct device_desc_devtree device_desc_devtree_t; struct device_desc { int type; union { device_desc_pci_t pci; device_desc_devtree_t devtree; }; uint32_t args[4]; }; typedef struct device_desc device_desc_t; #define DRIVER_ID_EMPTY (0xff) struct device { int id; int type; bool is_virtual; int driver_id; device_desc_t device_desc; }; typedef struct device device_t; #endif // _KERNEL_DRIVERS_BITS_DEVICE_H
willmexe/opuntiaOS
kernel/include/libkern/bits/sys/wait.h
#ifndef _KERNEL_LIBKERN_BITS_SYS_WAIT_H #define _KERNEL_LIBKERN_BITS_SYS_WAIT_H #define WNOHANG 0x1 #define WUNTRACED 0x2 #endif // _KERNEL_LIBKERN_BITS_SYS_WAIT_H
willmexe/opuntiaOS
libs/libui/include/libui/Constants/Layout.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once namespace UI::LayoutConstraints { enum class Axis { Horizontal, Vertical }; } // namespace UI::LayoutConstraints
willmexe/opuntiaOS
servers/window_server/src/Constants/Colors.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include <sys/types.h> namespace WinServer { enum Color { Shadow = 0xf4000000, InactiveText = 0x7B7E78, }; } // namespace WinServer
willmexe/opuntiaOS
kernel/kernel/libkern/scanf.c
#include <libkern/ctype.h> #include <libkern/scanf.h> #include <libkern/stdarg.h> typedef scanf_lookupch_callback _lookupch_callback; typedef scanf_getch_callback _getch_callback; typedef scanf_putch_callback _putch_callback; static int _scanf_u32(_lookupch_callback lookupch, _getch_callback getch, void* callback_params, uint32_t* val) { int read = 0; uint32_t res = 0; while (isdigit(lookupch(callback_params))) { res *= 10; char ch = getch(callback_params); res += (ch - '0'); read++; } *val = res; return read; } static int _scanf_i32(_lookupch_callback lookupch, _getch_callback getch, void* callback_params, int* val) { int read = 0; bool negative = 0; if (lookupch(callback_params) == '-') { negative = true; getch(callback_params); read++; } uint32_t tmp_u32; int read_imm = _scanf_u32(lookupch, getch, callback_params, &tmp_u32); if (!read_imm) { return 0; } read += read_imm; int tmp_i32 = (int)tmp_u32; if (negative) { tmp_i32 = -tmp_i32; } *val = tmp_i32; return read; } static int _scanf_internal(const char* format, _lookupch_callback lookupch, _getch_callback getch, void* callback_params, va_list arg) { const char* p = format; int passed = 0; while (*p) { int l_arg = 0; int h_arg = 0; while (isspace(*p)) { p++; } if (*p == '%' && *(p + 1)) { // Reading arguments parse_args: p++; switch (*p) { case 'l': l_arg++; if (*(p + 1)) { goto parse_args; } break; case 'h': h_arg++; if (*(p + 1)) { goto parse_args; } break; default: break; } while (isspace(lookupch(callback_params))) { getch(callback_params); passed++; } switch (*p) { case 'i': case 'd': if (!l_arg) { int* value = va_arg(arg, int*); int read = _scanf_i32(lookupch, getch, callback_params, value); passed += read; } break; case 'u': if (!l_arg) { uint32_t* value = va_arg(arg, uint32_t*); int read = _scanf_u32(lookupch, getch, callback_params, value); passed += read; } break; case 's': { int rv = 0; char* value = va_arg(arg, char*); while (!isspace(lookupch(callback_params))) { char ch = getch(callback_params); value[rv++] = ch; passed++; } value[rv] = '\0'; break; } } p++; } else { while (isspace(lookupch(callback_params))) { getch(callback_params); passed++; } if (lookupch(callback_params) != *p) { break; } p++; getch(callback_params); passed++; } } return passed; } struct buffer_params { const char* buf; }; typedef struct buffer_params buffer_params_t; static int lookup_callback_buffer(void* callback_params) { buffer_params_t* bp = (buffer_params_t*)callback_params; if (!bp->buf) { return EOF; } return *bp->buf; } static int getch_callback_buffer(void* callback_params) { buffer_params_t* bp = (buffer_params_t*)callback_params; if (!bp->buf) { return EOF; } char res = *bp->buf; bp->buf++; return res; } int vsscanf(const char* buf, const char* format, va_list arg) { buffer_params_t bp = { .buf = buf }; return _scanf_internal(format, lookup_callback_buffer, getch_callback_buffer, (void*)&bp, arg); } int sscanf(const char* buf, const char* format, ...) { va_list ap; va_start(ap, format); int count = vsscanf(buf, format, ap); va_end(ap); return count; } int scanf_engine(const char* format, _lookupch_callback lookupch, _getch_callback getch, void* callback_params, va_list arg) { return _scanf_internal(format, lookupch, getch, callback_params, arg); }
willmexe/opuntiaOS
libs/libc/include/opuntia/shared_buffer.h
<filename>libs/libc/include/opuntia/shared_buffer.h #ifndef _LIBC_SYS_SHARED_BUFFER_H #define _LIBC_SYS_SHARED_BUFFER_H #include <bits/sys/select.h> #include <bits/time.h> #include <stddef.h> #include <sys/cdefs.h> #include <sys/types.h> __BEGIN_DECLS int shared_buffer_create(uint8_t** buffer, size_t size); int shared_buffer_get(int id, uint8_t** buffer); int shared_buffer_free(int id); __END_DECLS #endif // _LIBC_SYS_SHARED_BUFFER_H
willmexe/opuntiaOS
test/kernel/fs/procfs/main.c
<filename>test/kernel/fs/procfs/main.c #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char** argv) { int fd = 0; fd = open("/proc/stat", O_RDWR); if (fd > 0) { TestErr("Opened /proc/stat for write"); } fd = open("/proc/stat", O_RDONLY); if (fd <= 0) { TestErr("Can't open /proc/stat for read"); } return 0; }
willmexe/opuntiaOS
libs/libfoundation/include/libfoundation/EventReceiver.h
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include <libfoundation/Event.h> #include <memory> namespace LFoundation { class EventReceiver { public: EventReceiver() = default; ~EventReceiver() = default; virtual void receive_event(std::unique_ptr<Event> event) { } private: }; } // namespace LFoundation
willmexe/opuntiaOS
libs/libc/include/stddef.h
<filename>libs/libc/include/stddef.h #ifndef _LIBC_STDDEF_H #define _LIBC_STDDEF_H #include <sys/cdefs.h> #include <sys/types.h> __BEGIN_DECLS #ifdef __i386__ #if defined(__clang__) typedef unsigned int size_t; typedef int ssize_t; typedef int ptrdiff_t; #elif defined(__GNUC__) || defined(__GNUG__) typedef unsigned long size_t; typedef long ssize_t; typedef long ptrdiff_t; #endif #elif __arm__ typedef unsigned int size_t; typedef int ssize_t; typedef int ptrdiff_t; #endif #define NULL ((void*)0) __END_DECLS #endif // _LIBC_STDDEF_H
willmexe/opuntiaOS
kernel/include/libkern/bits/sys/utsname.h
<gh_stars>100-1000 #ifndef _KERNEL_LIBKERN_BITS_SYS_UTSNAME_H #define _KERNEL_LIBKERN_BITS_SYS_UTSNAME_H #define UTSNAME_ENTRY_LEN 65 struct utsname { char sysname[UTSNAME_ENTRY_LEN]; char nodename[UTSNAME_ENTRY_LEN]; char release[UTSNAME_ENTRY_LEN]; char version[UTSNAME_ENTRY_LEN]; char machine[UTSNAME_ENTRY_LEN]; }; typedef struct utsname utsname_t; #endif // _KERNEL_LIBKERN_BITS_SYS_UTSNAME_H
willmexe/opuntiaOS
userland/utilities/touch/main.c
#include <fcntl.h> #include <sys/stat.h> #include <unistd.h> int main(int argc, char** argv) { int fd; if (argc < 2) { write(1, "Usage: touch files...\n", 22); return 0; } mode_t std_mode = S_IFREG | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; for (int i = 1; i < argc; i++) { if ((fd = creat(argv[i], std_mode)) < 0) { write(1, "touch: failed to create\n", 24); break; } close(fd); } return 0; }
willmexe/opuntiaOS
kernel/include/tasking/cpu.h
<filename>kernel/include/tasking/cpu.h /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef _KERNEL_TASKING_CPU_H #define _KERNEL_TASKING_CPU_H #include <drivers/generic/fpu.h> #include <platform/generic/cpu.h> #include <tasking/bits/sched.h> #include <tasking/proc.h> #include <tasking/thread.h> #define RUNNING_THREAD (THIS_CPU->running_thread) static inline void cpu_enter_kernel_space() { THIS_CPU->current_state = CPU_IN_KERNEL; } static inline void cpu_leave_kernel_space() { THIS_CPU->current_state = CPU_IN_USERLAND; } static inline void cpu_tick() { if (THIS_CPU->running_thread->process->is_kthread) { THIS_CPU->stat_system_and_idle_ticks++; } else { THIS_CPU->stat_user_ticks++; } } #endif // _KERNEL_TASKING_CPU_H
willmexe/opuntiaOS
kernel/kernel/drivers/aarch32/pl031.c
/* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <drivers/aarch32/pl031.h> #include <drivers/devtree.h> #include <fs/devfs/devfs.h> #include <fs/vfs.h> #include <libkern/bits/errno.h> #include <libkern/libkern.h> #include <libkern/log.h> #include <mem/kmemzone.h> #include <mem/vmm.h> #include <tasking/tasking.h> // #define DEBUG_PL031 static kmemzone_t mapped_zone; static volatile pl031_registers_t* registers; static inline uintptr_t _pl031_mmio_paddr() { devtree_entry_t* device = devtree_find_device("pl031"); if (!device) { kpanic("PL031: Can't find device in the tree."); } return (uintptr_t)device->paddr; } static inline int _pl031_map_itself() { uintptr_t mmio_paddr = _pl031_mmio_paddr(); mapped_zone = kmemzone_new(sizeof(pl031_registers_t)); vmm_map_page(mapped_zone.start, mmio_paddr, MMU_FLAG_DEVICE); registers = (pl031_registers_t*)mapped_zone.ptr; return 0; } void pl031_install() { if (_pl031_map_itself()) { #ifdef DEBUG_PL031 log_error("PL031: Can't map itself!"); #endif return; } } uint32_t pl031_read_rtc() { return registers->data; } devman_register_driver_installation(pl031_install);
willmexe/opuntiaOS
kernel/kernel/drivers/x86/ata.c
<filename>kernel/kernel/drivers/x86/ata.c /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <drivers/x86/ata.h> #include <libkern/bits/errno.h> ata_t _ata_drives[MAX_DEVICES_COUNT]; static uint8_t _ata_drives_count = 0; static driver_desc_t _ata_driver_info(); static uint8_t _ata_gen_drive_head_register(bool is_lba, bool is_master, uint8_t head); static int ata_write(device_t* device, uint32_t sector, uint8_t* data, uint32_t size); static int ata_read(device_t* device, uint32_t sector, uint8_t* read_data); static int ata_flush(device_t* device); static uint32_t ata_get_capacity(device_t* device); /** * Drive/Head register: * 1 lba, 1, drv, head [0-3] * drv: 0 if master else 1 * lba: is lba access */ static uint8_t _ata_gen_drive_head_register(bool is_lba, bool is_master, uint8_t head) { uint8_t res = 0xA0; res |= ((is_lba & 0x1) << 6); res |= ((is_master & 0x1) << 4); res |= (head & 15); return res; } static driver_desc_t _ata_driver_info() { driver_desc_t ata_desc = { 0 }; ata_desc.type = DRIVER_STORAGE_DEVICE; ata_desc.listened_device_mask = DEVICE_STORAGE; ata_desc.system_funcs.init_with_dev = ata_init_with_dev; ata_desc.functions[DRIVER_STORAGE_ADD_DEVICE] = ata_init_with_dev; ata_desc.functions[DRIVER_STORAGE_READ] = ata_read; ata_desc.functions[DRIVER_STORAGE_WRITE] = ata_write; ata_desc.functions[DRIVER_STORAGE_FLUSH] = ata_flush; ata_desc.functions[DRIVER_STORAGE_CAPACITY] = ata_get_capacity; return ata_desc; } int ata_init_with_dev(device_t* dev) { if (dev->device_desc.type != DEVICE_DESC_PCI) { return -1; } if (dev->device_desc.pci.class_id != 0x01) { return -1; } if (dev->device_desc.pci.subclass_id != 0x05) { return -1; } bool is_master = dev->device_desc.pci.port_base >> 31; uint16_t port = dev->device_desc.pci.port_base & 0xFFF; ata_init(&_ata_drives[dev->id], port, is_master); if (ata_indentify(&_ata_drives[dev->id])) { kprintf("Device added to ata driver\n"); } else { return -1; } return 0; } void ata_install() { // registering driver and passing info to it devman_register_driver(_ata_driver_info(), "ata86"); } devman_register_driver_installation(ata_install); void ata_init(ata_t* ata, uint32_t port, bool is_master) { ata->is_master = is_master; ata->port.data = port; ata->port.error = port + 0x1; ata->port.sector_count = port + 0x2; ata->port.lba_lo = port + 0x3; ata->port.lba_mid = port + 0x4; ata->port.lba_hi = port + 0x5; ata->port.device = port + 0x6; ata->port.command = port + 0x7; ata->port.control = port + 0x206; } bool ata_indentify(ata_t* ata) { port_8bit_out(ata->port.device, ata->is_master ? 0xA0 : 0xB0); port_8bit_out(ata->port.sector_count, 0); port_8bit_out(ata->port.lba_lo, 0); port_8bit_out(ata->port.lba_mid, 0); port_8bit_out(ata->port.lba_hi, 0); port_8bit_out(ata->port.command, 0xEC); // check the acceptance of a command uint8_t status = port_8bit_in(ata->port.command); if (status == 0x00) { kprintf("Cmd isn't accepted"); return false; } // waiting for processing // while BSY is on while ((status & 0x80) == 0x80) { status = port_8bit_in(ata->port.command); } // check if drive isn't ready to transer DRQ if ((status & 0x08) != 0x08) { kprintf("Doesn't ready to transfer DRQ"); return false; } // transfering 256 bytes of data for (int i = 0; i < 256; i++) { uint16_t data = port_16bit_in(ata->port.data); if (i == 1) { ata->cylindres = data; } if (i == 3) { ata->heads = data; } if (i == 6) { ata->sectors = data; } if (i == 49) { if (((data >> 8) & 0x1) == 1) { ata->dma = true; } if (((data >> 9) & 0x1) == 1) { ata->lba = true; } } if (i == 60) { ata->capacity = data; } if (i == 61) { ata->capacity |= ((uint32_t)data) << 16; } } return true; } int ata_write(device_t* device, uint32_t sectorNum, uint8_t* data, uint32_t size) { ata_t* dev = &_ata_drives[device->id]; uint8_t dev_config = _ata_gen_drive_head_register(true, !dev->is_master, 0); port_8bit_out(dev->port.device, dev_config); port_8bit_out(dev->port.sector_count, 1); port_8bit_out(dev->port.lba_lo, sectorNum & 0x000000FF); port_8bit_out(dev->port.lba_mid, (sectorNum & 0x0000FF00) >> 8); port_8bit_out(dev->port.lba_hi, (sectorNum & 0x00FF0000) >> 16); port_8bit_out(dev->port.error, 0); port_8bit_out(dev->port.command, 0x31); // waiting for processing // while BSY is on and no Errors uint8_t status = port_8bit_in(dev->port.command); while (((status >> 7) & 1) == 1 && ((status >> 0) & 1) != 1) { status = port_8bit_in(dev->port.command); } // check if drive isn't ready to transer DRQ if (((status >> 0) & 1) == 1) { kprintf("Error"); return -EBUSY; } for (int i = 0; i < size; i += 2) { uint16_t db = (data[i + 1] << 8) + data[i]; port_16bit_out(dev->port.data, db); } for (int i = size / 2; i < 256; i++) { port_16bit_out(dev->port.data, 0); } return ata_flush(device); } int ata_read(device_t* device, uint32_t sectorNum, uint8_t* read_data) { ata_t* dev = &_ata_drives[device->id]; uint8_t dev_config = _ata_gen_drive_head_register(true, !dev->is_master, 0); port_8bit_out(dev->port.device, dev_config); port_8bit_out(dev->port.sector_count, 1); port_8bit_out(dev->port.lba_lo, sectorNum & 0x000000FF); port_8bit_out(dev->port.lba_mid, (sectorNum & 0x0000FF00) >> 8); port_8bit_out(dev->port.lba_hi, (sectorNum & 0x00FF0000) >> 16); port_8bit_out(dev->port.error, 0); port_8bit_out(dev->port.command, 0x21); // waiting for processing // while BSY is on and no Errors uint8_t status = port_8bit_in(dev->port.command); while (((status >> 7) & 1) == 1 && ((status >> 0) & 1) != 1) { status = port_8bit_in(dev->port.command); } // check if drive isn't ready to transer DRQ if (((status >> 0) & 1) == 1) { kprintf("Error"); return -EBUSY; } if (((status >> 3) & 1) == 0) { kprintf("No DRQ"); return -ENODEV; } for (int i = 0; i < 256; i++) { uint16_t data = port_16bit_in(dev->port.data); read_data[2 * i + 1] = (data >> 8) & 0xFF; read_data[2 * i + 0] = (data >> 0) & 0xFF; } return 0; } int ata_flush(device_t* device) { ata_t* dev = &_ata_drives[device->id]; uint8_t dev_config = _ata_gen_drive_head_register(true, !dev->is_master, 0); port_8bit_out(dev->port.device, dev_config); port_8bit_out(dev->port.command, 0xE7); uint8_t status = port_8bit_in(dev->port.command); if (status == 0x00) { return -ENODEV; } while (((status >> 7) & 1) == 1 && ((status >> 0) & 1) != 1) { status = port_8bit_in(dev->port.command); } if (status & 0x01) { return -EBUSY; } return 0; } /* Returns a disk size in bytes */ uint32_t ata_get_capacity(device_t* device) { // FIXME: Sector size is hard-coded. ata_t* dev = &_ata_drives[device->id]; return dev->capacity * 512; } uint8_t ata_get_drives_count() { return _ata_drives_count; }
willmexe/opuntiaOS
test/bench/common.h
<reponame>willmexe/opuntiaOS #pragma once #include <ctime> #include <sys/time.h> #define RUN_BENCH(name, x) for (bench_run = 0, bench_pno = bench_no, gettimeofday(&tv, &tz); bench_run < x; gettimeofday(&ttv, &tz), printf("[BENCH][%s] %d (usec)\n", name, to_usec()), fflush(stdout), bench_run++, gettimeofday(&tv, &tz)) extern int bench_pno; extern int bench_no; extern int bench_run; extern timeval_t tv, ttv; extern timezone_t tz; static inline int to_usec() { int sec = ttv.tv_sec - tv.tv_sec; int diff = ttv.tv_usec - tv.tv_usec; return sec * 1000000 + diff; } void bench_pngloader();
willmexe/opuntiaOS
libs/libc/include/sys/stat.h
#ifndef _LIBC_SYS_STAT_H #define _LIBC_SYS_STAT_H #include <bits/sys/stat.h> #include <sys/cdefs.h> #include <sys/types.h> __BEGIN_DECLS int mkdir(const char* path); int fstat(int nfds, fstat_t* stat); __END_DECLS #endif
willmexe/opuntiaOS
kernel/kernel/mem/kswapd.c
<gh_stars>0 /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <fs/vfs.h> #include <libkern/bits/errno.h> #include <libkern/log.h> #include <mem/kswapd.h> #include <mem/swapfile.h> #include <mem/vmm.h> #include <syscalls/handlers.h> #include <tasking/tasking.h> // #define KSWAPD_DEBUG #define KSWAPD_SLEEPTIME (3) // seconds. #define KSWAPD_SWAP_PER_PID_THRESHOLD (2) #define KSWAPD_SWAP_PER_RUN_THRESHOLD (4) static kmemzone_t _mapzone; static uintptr_t _mmaped_ptable; static int moved_out_pages_per_run = 0; static int moved_out_pages_per_pid = 0; static int last_pid = 0, last_pti = 0; extern proc_t proc[MAX_PROCESS_COUNT]; static int map_ptable(table_desc_t* ptable_desc) { uintptr_t ptable_paddr = PAGE_START((uintptr_t)table_desc_get_frame(*ptable_desc)); if (_mmaped_ptable == ptable_paddr) { return 0; } int err = vmm_map_page(_mapzone.start, ptable_paddr, MMU_FLAG_PERM_READ | MMU_FLAG_PERM_WRITE); if (err) { return err; } _mmaped_ptable = ptable_paddr; return 0; } inline static void after_page_swap() { moved_out_pages_per_run++; moved_out_pages_per_pid++; } static void do_sleep() { moved_out_pages_per_run = 0; ksys1(SYS_NANOSLEEP, KSWAPD_SLEEPTIME); } static int find_victim(proc_t* p, pdirectory_t* pdir) { lock_acquire(&p->vm_lock); ptable_t* ptable_map_zone = (ptable_t*)_mapzone.ptr; const size_t ptables_per_page = VMM_PAGE_SIZE / PTABLE_SIZE; const size_t table_coverage = VMM_PAGE_SIZE * VMM_TOTAL_PAGES_PER_TABLE; for (int pti = last_pti; pti < VMM_KERNEL_TABLES_START; pti++, last_pti++) { table_desc_t* ptable_desc = &pdir->entities[pti]; if (!table_desc_has_attrs(*ptable_desc, TABLE_DESC_PRESENT)) { continue; } map_ptable(ptable_desc); for (int pgi = 0; pgi < VMM_TOTAL_PAGES_PER_TABLE; pgi++) { page_desc_t* ppage_desc = &ptable_map_zone[pti].entities[pgi]; if (!page_desc_has_attrs(*ppage_desc, PAGE_DESC_PRESENT)) { continue; } uintptr_t victim_vaddr = table_coverage * pti + VMM_PAGE_SIZE * pgi; memzone_t* zone = memzone_find(p, victim_vaddr); #ifdef KSWAPD_DEBUG log("[kswapd] (pid %d) Find victim at %x", p->pid, victim_vaddr); #endif vmm_swap_page(&ptable_map_zone[pti], zone, victim_vaddr); after_page_swap(); if (moved_out_pages_per_pid >= KSWAPD_SWAP_PER_PID_THRESHOLD) { lock_release(&p->vm_lock); return 0; } } } lock_release(&p->vm_lock); return 0; } void kswapd() { _mapzone = kmemzone_new(VMM_PAGE_SIZE); for (;;) { if (pmm_get_free_space_in_kb() * 4 >= pmm_get_ram_in_kb()) { #ifdef KSWAPD_DEBUG log("[kswapd] Skip pass, %d free of %d, <75%% busy", pmm_get_free_space_in_kb(), pmm_get_ram_in_kb()); #endif goto sleep; } proc_t* p; for (int i = last_pid; i < tasking_get_proc_count(); i++, last_pid++) { p = &proc[i]; if (p->status == PROC_ALIVE) { find_victim(p, p->pdir); } last_pti = 0; moved_out_pages_per_pid = 0; if (moved_out_pages_per_run >= KSWAPD_SWAP_PER_RUN_THRESHOLD) { goto sleep; } } last_pid = 0; sleep: do_sleep(); } }
willmexe/opuntiaOS
libs/libc/include/sys/utsname.h
#ifndef _LIBC_SYS_UTSNAME_H #define _LIBC_SYS_UTSNAME_H #include <bits/sys/utsname.h> #include <sys/cdefs.h> __BEGIN_DECLS int uname(utsname_t* buf); __END_DECLS #endif // _LIBC_SYS_UTSNAME_H
willmexe/opuntiaOS
userland/utilities/edit/viewer.c
<filename>userland/utilities/edit/viewer.c #include "viewer.h" #include "file.h" #include <stdlib.h> #include <unistd.h> int out_x, out_y; int cursor_x, cursor_y; char* displaying_buffer; int working_with_offset; static int _viewer_get_offset(int cx, int cy) { return cy * SCREEN_X + cx; } void viewer_clear_screen() { out_x = out_y = 0; write(STDOUT, "\x1b[2J", 4); write(STDOUT, "\x1b[H", 3); } void _viewer_set_cursor(int col, int row) { char buf[8]; buf[0] = '\x1b'; buf[1] = '['; int id = 2; col++, row++; if (row < 10) { buf[id++] = row + '0'; } else { buf[id++] = (row / 10) + '0'; buf[id++] = (row % 10) + '0'; } buf[id++] = ';'; if (col < 10) { buf[id++] = col + '0'; } else { buf[id++] = (col / 10) + '0'; buf[id++] = (col % 10) + '0'; } buf[id++] = 'H'; write(STDOUT, buf, id); } void _viewer_set_cursor_to_start() { write(STDOUT, "\x1b[H", 3); } void _viewer_new_line() { write(STDOUT, "\n", 1); } char _viewer_has_space_to_print(char c) { if (c == '\n') { if (out_y == SCREEN_Y - 1) { return 0; } } else { if (out_x == SCREEN_X - 1 && out_y == SCREEN_Y - 1) { return 0; } } return 1; } char _viewer_display_one(char c) { if (_viewer_has_space_to_print(c)) { if (c == '\n') { out_x = 0; out_y++; _viewer_new_line(); } else { write(STDOUT, &c, 1); out_x++; if (out_x == SCREEN_X) { out_x = 0; out_y++; } } return 1; } return 0; } static void _log_dec(uint32_t dec) { uint32_t pk = 1000000000; char was_not_zero = 0; while (pk > 0) { uint32_t pp = dec / pk; if (was_not_zero || pp > 0) { char ccp = pp + '0'; write(1, &ccp, 1); was_not_zero = 1; } dec -= pp * pk; pk /= 10; } if (!was_not_zero) { write(1, "0", 1); } } void viewer_display(char* buf, int start, int len) { displaying_buffer = buf; int last_cx = 0; viewer_clear_screen(); for (int i = start; i < start + len; i++) { if (out_x <= cursor_x && out_y == cursor_y) { working_with_offset = i; last_cx = out_x; } if (!_viewer_display_one(buf[i])) { goto out; } } while (out_y < SCREEN_Y - 1) { out_y++; write(STDOUT, "\n~", 2); } out: cursor_x = last_cx; _viewer_set_cursor(cursor_x, cursor_y); } void viewer_cursor_left() { if (cursor_x) { cursor_x--; working_with_offset--; _viewer_set_cursor(cursor_x, cursor_y); } } void viewer_cursor_right() { if (displaying_buffer) { if (displaying_buffer[working_with_offset] != '\n') { cursor_x++; working_with_offset++; _viewer_set_cursor(cursor_x, cursor_y); } } } void viewer_cursor_up() { if (!cursor_y) { file_scroll_up(); } else { working_with_offset -= file_line_len_backwards(working_with_offset); cursor_y--; int new_line_len = file_line_len_backwards(working_with_offset) - 1; if (cursor_x >= new_line_len) { cursor_x = (new_line_len) % SCREEN_X; } working_with_offset -= (new_line_len); working_with_offset += cursor_x; _viewer_set_cursor(cursor_x, cursor_y); } } void viewer_cursor_down() { if (cursor_y == SCREEN_Y - 1) { file_scroll_down(); } else { working_with_offset += file_line_len(working_with_offset); cursor_y++; if (cursor_x >= file_line_len(working_with_offset)) { cursor_x = file_line_len(working_with_offset) - 1; } working_with_offset += cursor_x; _viewer_set_cursor(cursor_x, cursor_y); } } void viewer_cursor_next() { cursor_x++; if (cursor_x == SCREEN_X) { cursor_y++; cursor_x = 0; } } int viewer_get_cursor_offset_in_file() { return working_with_offset; } void viewer_enter_menu_mode() { write(STDOUT, "\x1b[25;1H", 7); } void viewer_leave_menu_mode() { /* FIXME: Restore cursor to prev position */ _viewer_set_cursor_to_start(); }
willmexe/opuntiaOS
userland/utilities/kill/main.c
#include <fcntl.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char** argv) { if (argc <= 2) { return 0; } printf("%d %d", atoi(argv[1]), atoi(argv[2])); kill(atoi(argv[1]), atoi(argv[2])); return 0; }
willmexe/opuntiaOS
servers/window_server/src/Devices/Screen.h
<gh_stars>100-1000 /* * Copyright (C) 2020-2022 The opuntiaOS Project Authors. * + Contributed by <NAME> <<EMAIL>> * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #pragma once #include <libg/Color.h> #include <libg/PixelBitmap.h> #include <memory> namespace WinServer { class Screen { public: inline static Screen& the() { extern Screen* s_WinServer_Screen_the; return *s_WinServer_Screen_the; } Screen(); void swap_buffers(); inline size_t width() { return m_bounds.width(); } inline size_t height() const { return m_bounds.height(); } inline LG::Rect& bounds() { return m_bounds; } inline const LG::Rect& bounds() const { return m_bounds; } inline uint32_t depth() const { return m_depth; } inline LG::PixelBitmap& write_bitmap() { return *m_write_bitmap_ptr; } inline const LG::PixelBitmap& write_bitmap() const { return *m_write_bitmap_ptr; } inline LG::PixelBitmap& display_bitmap() { return *m_display_bitmap_ptr; } inline const LG::PixelBitmap& display_bitmap() const { return *m_display_bitmap_ptr; } private: int m_screen_fd; LG::Rect m_bounds; uint32_t m_depth; int m_active_buffer; LG::PixelBitmap m_write_bitmap; LG::PixelBitmap m_display_bitmap; std::unique_ptr<LG::PixelBitmap> m_write_bitmap_ptr { nullptr }; std::unique_ptr<LG::PixelBitmap> m_display_bitmap_ptr { nullptr }; }; } // namespace WinServer
willmexe/opuntiaOS
libs/libc/ctype/ctype.c
#include <ctype.h> const char __ctypes[256] = { _C, _C, _C, _C, _C, _C, _C, _C, _C, _C | _S, _C | _S, _C | _S, _C | _S, _C | _S, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C, _C, (char)(_S | _B), _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _N, _N, _N, _N, _N, _N, _N, _N, _N, _N, _P, _P, _P, _P, _P, _P, _P, _U | _X, _U | _X, _U | _X, _U | _X, _U | _X, _U | _X, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _P, _P, _P, _P, _P, _P, _L | _X, _L | _X, _L | _X, _L | _X, _L | _X, _L | _X, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _P, _P, _P, _P, _C };
willmexe/opuntiaOS
userland/utilities/edit/file.c
#include "file.h" #include "viewer.h" #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <unistd.h> file_view_area_t view_area; static int _file_get_size_of_first_line() { int res = 0; for (int i = view_area.start; i < view_area.data_len; i++) { res++; if (view_area.buffer[i] == '\n') { return res; } } return view_area.data_len - view_area.start; } static void _file_find_start(uint32_t old_offset) { old_offset -= 2; while (old_offset > view_area.offset) { if (view_area.buffer[old_offset - view_area.offset] == '\n') { view_area.start = old_offset - view_area.offset + 1; return; } old_offset--; } view_area.start = 0; } static inline int _file_remaining_size() { return view_area.data_len - view_area.start; } static inline int _file_is_last_line_in_buf() { return 1; } int _file_buf_flush() { /* FIXME: for now we think that we have all file in our buffer */ lseek(view_area.fd, view_area.offset, SEEK_SET); view_area.data_len = write(view_area.fd, view_area.buffer, view_area.data_len); return 0; } int _file_buf_reload(int offset_in_file) { /* TODO: Get real file size */ view_area.buf_len = 4000; view_area.buffer = (char*)malloc(view_area.buf_len); view_area.start = 0; view_area.offset = offset_in_file; view_area.was_changed = 0; lseek(view_area.fd, view_area.offset, SEEK_SET); view_area.data_len = read(view_area.fd, view_area.buffer, view_area.buf_len); if (view_area.data_len < 0) { return -1; } return 0; } int file_line_len(int offset) { int res = 0; for (int i = offset; i < view_area.data_len; i++) { res++; if (view_area.buffer[i] == '\n') { return res; } } return view_area.data_len - view_area.start; } int file_line_len_backwards(int offset) { int res = 1; for (int i = offset - 1; i >= view_area.offset; i--) { if (view_area.buffer[i] == '\n') { return res; } res++; } return offset; } int file_open(char* path) { int n; if ((view_area.fd = open(path, O_RDWR)) < 0) { return -1; } _file_buf_reload(0); if (view_area.data_len < 0) { return -1; } viewer_display(view_area.buffer, view_area.start, _file_remaining_size()); return view_area.fd; } void file_exit() { close(view_area.fd); } /** * file_paste_char pastes and flush updates to disk. */ int file_paste_char(char c, int offset) { view_area.was_changed = 1; view_area.data_len++; memmove(view_area.buffer + offset + 1, view_area.buffer + offset, view_area.data_len - offset - 1); view_area.buffer[offset] = c; viewer_cursor_next(); viewer_display(view_area.buffer, view_area.start, _file_remaining_size()); } /** * file_paste_char pastes and flush updates to disk. */ int file_save() { if (view_area.was_changed) { _file_buf_flush(); } } /** * Scroll functions update file buffer, to contain line scrolled to. */ int file_scroll_up() { if (view_area.line == 0 || view_area.start == 0) { return -1; } view_area.line--; int old_offset_in_file = view_area.offset + view_area.start; int offset_in_file = view_area.offset + view_area.start - SCREEN_X; if (offset_in_file < 0) { offset_in_file = 0; } if (offset_in_file >= view_area.offset) { view_area.start = offset_in_file - view_area.offset; _file_find_start(old_offset_in_file); viewer_display(view_area.buffer, view_area.start, _file_remaining_size()); } else { _file_buf_reload(offset_in_file); _file_find_start(old_offset_in_file); viewer_display(view_area.buffer, view_area.start, _file_remaining_size()); } } int file_scroll_down() { int line_size = _file_get_size_of_first_line(); view_area.start += line_size; if (!_file_is_last_line_in_buf()) { _file_buf_reload(view_area.offset + view_area.start); } viewer_display(view_area.buffer, view_area.start, _file_remaining_size()); view_area.line++; }