Compare commits

...

2078 Commits
main ... main

Author SHA1 Message Date
Bananymous a89fd95864 aoc2024: Implement day14 solution 2024-12-14 23:52:22 +02:00
Bananymous 9ba9469bb1 aoc2024: Optimize day12
There is actually no need for hash maps
2024-12-14 03:58:11 +02:00
Bananymous 4dbf173ed4 aoc2024: Implement day13 solution 2024-12-14 03:17:55 +02:00
Bananymous 8936cca9cf aoc2024: Implement day12 solution 2024-12-12 17:03:31 +02:00
Bananymous 1c704680a8 aoc2024: Implement day11 solution
Finally we got a problem where trivial solution does not work
2024-12-11 09:05:52 +02:00
Bananymous 3e36834751 aoc2024: Implement day10 solution 2024-12-10 17:26:39 +02:00
Bananymous 581caff638 BuildSystem: Install base sysroot only once
Base sysroot is not only installed when creating the syroot directory
for the first time. To get it installed again, you have to either delete
the old sysroot diretory or run clean target

This makes it so its actually possible to edit files specified in the
base sysroot without random hacks.
2024-12-10 16:44:59 +02:00
Bananymous 8b612ba547 aoc2024: Implement day9 solution
part2 is kinda unoptimized, it should not use expanded input, but I did
not feel like rewriting my parsing code :)
2024-12-09 16:20:21 +02:00
Bananymous 90820f24a4 BAN: Make HashMap::{insert,emplace}{,_or_assign} return an iterator
This reduces the number of lookups done into the hash map :)
2024-12-09 03:37:13 +02:00
Bananymous 12a37500b0 Kernel: Fix triple-fault in vitual tty
This was happening when printing non-utf8 data while having virtual tty
as the debug console.
2024-12-09 03:35:51 +02:00
Bananymous 3ae6ffdb01 ports/tcc: Create usr/include/sys/ucontext.h
This file does not have to contain anything, tcc just requires that it
exists.
2024-12-08 20:42:04 +02:00
Bananymous c4e76b391e aoc2024: Implement day8 solution
I did not feel like optimizing this one, so its a O(n^2) looking over
every cell in the grid. Performs way faster than my optimized day6
solution tho :DD
2024-12-08 16:14:39 +02:00
Bananymous cf9ba737f0 aoc2024: Implement day7 solution 2024-12-07 07:34:21 +02:00
Bananymous 2d0690ae2d Kernel: Cleanup most of syscalls dealing with files 2024-12-07 05:33:04 +02:00
Bananymous 71a2d0971f Kernel: Remove unnecessary reverse from VFS file search 2024-12-07 01:41:23 +02:00
Bananymous 3bf57e0748 Kernel: Add . and .. entries to /proc/<pid> directories
This allows to run `cd ..` in one of these directories
2024-12-07 01:37:40 +02:00
Bananymous fbc0319cb6 aoc2024: Implement day6 solution 2024-12-07 01:08:08 +02:00
Bananymous e8bc3c2d80 General: Add intructions to run doom to README 2024-12-06 20:25:09 +02:00
Bananymous fe192e07fb General: Update README feature list 2024-12-05 19:49:38 +02:00
Bananymous 51b6329c86 Kernel: Make backspace \b and delete 0x7F
This is what `vim` seems to expect and imo makes way more sense.
2024-12-05 17:34:37 +02:00
Bananymous 72d9e4c1e7 Kernel: Remove accidental debug prints from fcntl syscall 2024-12-05 17:34:32 +02:00
Bananymous cb7d5c9d09 userspace: Implement `dirname` utility 2024-12-05 08:30:30 +02:00
Bananymous 097ab82529 userspace: Implement `basename` utility 2024-12-05 08:30:06 +02:00
Bananymous ccb603d77f aoc2024: Implement day5 solution 2024-12-05 08:16:34 +02:00
Bananymous d7b02db832 aoc2024: Implement day4 solution 2024-12-05 08:16:12 +02:00
Bananymous 56cc5da9fb userspace: Implement `ln` utility 2024-12-05 08:14:45 +02:00
Bananymous 1903079f96 LibC: Fix popen to return correct return value instead of -1 2024-12-05 06:47:10 +02:00
Bananymous b6d0950ee9 LibC: Fix strftime implementation
This implementation now passes every musl-libc test :)
2024-12-05 06:46:31 +02:00
Bananymous c1a32a4041 LibC: Implement mktime 2024-12-05 06:43:35 +02:00
Bananymous 8fa443879c ports: Add working tcc port!
You can actually now compile programs within banan-os!!
2024-12-04 21:23:09 +02:00
Bananymous 3a465cb94f DynamicLoader: Don't write to pltgot if it does not exist
This makes `tcc` compile runnable executables
2024-12-04 21:11:43 +02:00
Bananymous 49133dce48 Kernel: Make pipe check data every 100ms
This prevents dead lock if read-end does not detect data before blocking
why write-end already notified.
2024-12-03 21:54:27 +02:00
Bananymous 3b7b6beca5 ports: Allow using banan-os cmake toolchain file for ports 2024-12-03 20:29:38 +02:00
Bananymous 0dd81328ff ports: Unconditionally compile banan-os before building ports
This allows ports to use any banan-os library cleanly. Ninja is fast on
when everything is already compiled, so it doesn't add too much
overhead.
2024-12-03 20:29:38 +02:00
Bananymous a668593e6b ports: Don't update ports on every build
This makes using ports much less painful :)
2024-12-03 20:29:38 +02:00
Bananymous 3a5a22511a aoc2024: Implement day3 solution 2024-12-03 20:29:38 +02:00
Bananymous 86859267f0 ports/curl: remove libcurl.la file and use /dev/urandom 2024-12-03 20:29:38 +02:00
Bananymous 88c9f6d233 LibC: Make environ a weak alias
If user program defines its own global `environ` variable it will still
work :)
2024-12-03 20:29:38 +02:00
Bananymous 176693dd5a LibC: Implement wcscmp and wcsncmp 2024-12-03 16:21:54 +02:00
Bananymous 62bee04fc0 LibC: Implement dummy utime that just checks file existance 2024-12-03 16:21:18 +02:00
Bananymous 0a5aacfd87 Kernel: Allow open with O_CREAT bypass file permission checks
call to open(path, O_CREAT|O_EXCL|O_RDWR, 0444) should open file as
read-write although file is created as read-only on filesystem.
2024-12-03 16:17:34 +02:00
Bananymous 2dec3a6c95 Kernel: Add some sanity checks for existanse of threads 2024-12-03 16:16:50 +02:00
Bananymous fcc2814199 Kernel: Remove process from alarm list after it dies 2024-12-03 16:16:08 +02:00
Bananymous bc93d238dd LibC: Fix fread argument order in gethostname 2024-12-03 16:15:16 +02:00
Bananymous 79e2c5d48c LibC: Make FILE* reading buffered
There was no reason to perform a syscall for every byte read
2024-12-03 16:13:48 +02:00
Bananymous 0fab7ad63b Kernel: Fix SYS_FCNTL command handling :)
I had misunderstood how these work
2024-12-03 16:12:26 +02:00
Bananymous e6a2f55a59 Kernel: Closing file backed mmap wont expand the file size 2024-12-03 16:12:26 +02:00
Bananymous 67e9ca56ac Kernel: Create /dev/urandom -> /dev/random symlink
Most software uses urandom instead of random so this allows it work
normally.
2024-12-03 16:12:26 +02:00
Bananymous 713daf6cd3 Kernel/LibC: Add support for creating hardlinks 2024-12-03 16:12:26 +02:00
Bananymous 12abe81c6d ports/openssl: Add support for shared libraries 2024-12-03 01:46:59 +02:00
Bananymous fbcf10c86d LibC: Implement closelog and make syslog print to stddbg 2024-12-03 01:45:54 +02:00
Bananymous 415b20f884 LibC: Remove errno EEXISTS
I don't know why I had added this. It doesn't seem to be a thing on
either POSIX or linux. This was literally equivalent to EXIST
2024-12-02 20:13:38 +02:00
Bananymous d58ca5f37a Kernel/LibC: Implement symlink{,at} 2024-12-02 20:13:38 +02:00
Bananymous 11b6ee423e LibC: Define timezone structure 2024-12-02 20:13:37 +02:00
Bananymous a10ca47657 Kernel/LibC: Implement {,f}statvfs 2024-12-02 20:13:37 +02:00
Bananymous ad1f175a39 Kernel/LibC: Implement getppid 2024-12-02 20:13:37 +02:00
Bananymous fd3246113a LibC: Implement missing execlp and cleanup exec code 2024-12-02 20:13:37 +02:00
Bananymous b8013c883c LibC: Implement getpass 2024-12-02 20:13:37 +02:00
Bananymous ffcc4fd03a init: Make tty owned by logged in user 2024-12-02 20:13:37 +02:00
Bananymous 9d97964998 LibC: Fix getpwend gid field :) 2024-12-02 20:13:37 +02:00
Bananymous f0e54cdd51 LibC: Implement {asctime,ctime,gmtime,localtime}_r
Old non _r prefixed functions are now just wrappers around these new
functions :)
2024-12-02 20:13:37 +02:00
Bananymous 0360fd1efe LibC: Implement mk{,d,s}temp 2024-12-02 20:13:37 +02:00
Bananymous 4508e099ff LibC: Implement getpagesize 2024-12-02 20:13:37 +02:00
Bananymous 6ed1435aeb Kernel/LibC: Implement tcgetpgrp 2024-12-02 20:13:37 +02:00
Bananymous 6346e288ad LibC: Implement getrlimit 2024-12-02 20:13:37 +02:00
Bananymous d2b503910f userspace: fix aoc2023 and aoc2024 linking
This patch makes aoc projects not link against system libc but the cmake
libc target. This allows compilation before libc is installed
2024-12-02 20:13:37 +02:00
Bananymous 747c3b2a4b Kernel/LibC: Implement fsync 2024-12-02 20:13:37 +02:00
Bananymous cccb4e6d5e LibC: Implement basename and dirname 2024-12-02 20:13:37 +02:00
Bananymous f4c6afbdae LibC: Add definitions to LibC that are used by some ports 2024-12-02 20:13:37 +02:00
Bananymous d1ef380e6b LibC: Add missing definitions for ifreq members 2024-12-02 20:13:37 +02:00
Bananymous c02de2580d Kernel: Add support for SIOCGIFNAME ioctl 2024-12-02 20:13:37 +02:00
Bananymous a231f8587e aoc2024: Implement day2 solution 2024-12-02 20:13:37 +02:00
Bananymous 4149748766 Kernel: Add missing include in ACPI headers 2024-12-01 21:32:34 +02:00
Bananymous 783627c315 LibC: Fix time.h sigevent defition 2024-12-01 21:31:27 +02:00
Bananymous 1ff6aa1748 aoc2024: cleanup day1 solution 2024-12-01 07:58:40 +02:00
Bananymous 6662dc4a8d aoc2024: fix `full` runner search path for solutions 2024-12-01 07:58:40 +02:00
Bananymous d6b170e274 aoc2024: implement day1 solution 2024-12-01 07:41:25 +02:00
Bananymous 11cac7a646 aoc2024: prepare programming environment 2024-12-01 07:41:25 +02:00
Bananymous 9c2fcd745c ports/doom: Fix patch calling exit() to actually do what it says 2024-12-01 02:28:50 +02:00
Bananymous 04f49a6819 LibC: Implement {,v}dprintf 2024-11-27 21:33:10 +02:00
Bananymous d465ea2a67 LibC: Fix headers :) 2024-11-27 21:32:10 +02:00
Bananymous 1f5073d0ac ports/doom: Allow doomgeneric to write directly to window framebuffer
This prevents an unnecessary `malloc` of the framebuffer and a copy
every frame.
2024-11-27 13:39:21 +02:00
Bananymous 8dbbbc1a1a Kernel: Add command line option `readonly`
This prevents calls to write_sectors_impl and all dirty pages are always
kept in RAM.
2024-11-27 13:31:55 +02:00
Bananymous f985673dc3 LibGUI: Add API to retrieve Window's underlying framebuffer 2024-11-27 13:29:42 +02:00
Bananymous a6fc3cf7a6 Kernel: Clear AHCI pending interrupts
I have no idea why I did not have this in. All devices were just
constatly spamming interrupts like crazy :D
2024-11-26 01:39:11 +02:00
Bananymous 1941885cfd Kernel: Disable USB Mass Storage writing until I get it fixed
This prevented real hw from running for over 10-20 seconds. When first
disk sync happens after 10 secods, USB storage would enter faulty state
and not allow any io operations.

I have no idea what the problem is. If I don't send Reset command to the
USB device writing seems to work fine. I also don't think its part of
the Reset process either as reading works just fine.
2024-11-26 01:01:25 +02:00
Bananymous 88a86a9927 Kernel: Fix Thread destruction after sys_exit
This had undefined behaviour as Thread's (Processes's) PageTable was
destroyed before Thread had the change to destroy its own stacks that
lived on the PageTable.
2024-11-26 00:59:34 +02:00
Bananymous 48e030bca3 Kernel: Make PageTable non-copyable and non-movable
Also PageTable destructor now verifies that is has allocated something
instead of assuming paddr of 0.
2024-11-26 00:58:35 +02:00
Bananymous 6f118c1be1 BAN: Make HashMap key move constructible
This allows using non-copyable types as keys
2024-11-26 00:57:11 +02:00
Bananymous 7316eb87b8 BAN: Add requires expression for BAN::Formatter
This allows seeing syntax errors before compilation :)
2024-11-26 00:55:58 +02:00
Bananymous 5376236ab6 BAN: Make ErrorOr non-copyable
This makes avoiding accidentals copies easier :)
2024-11-26 00:55:12 +02:00
Bananymous 0af80d48ee BAN: Mark ByteSpan::slice as [[nodiscard]] 2024-11-26 00:54:35 +02:00
Bananymous 793c0368f2 Kernel: Refactor USB mass storage code
Also increment command timeout to 10 seconds so commands don't timeout
when they are not supposted to :)
2024-11-23 01:24:32 +02:00
Bananymous 076001462e Kernel: Implement AML ConcatOp 2024-11-22 22:23:07 +02:00
Bananymous d2e1d8ec13 Kernel: Implement AML NotOp 2024-11-22 22:22:44 +02:00
Bananymous 30ceaa6bef Kernel: Implement AML ToHexStringOp 2024-11-22 22:22:27 +02:00
Bananymous 0247d47a3d Kernel: Make USBMassStorage send_scsi_command templated
This allows passing ConstByteSpan when data will not be modified
2024-11-22 22:21:19 +02:00
Bananymous 480368c878 Kernel: Cleanup and fix xHCI interrupt handling
Always clear EventHandlerBusy bit and remove unnecessary nesting
2024-11-22 22:19:53 +02:00
Bananymous c1d8790623 Kernel: Don't allow sending commands to halted xHCI 2024-11-22 22:19:29 +02:00
Bananymous fb1ffec5bb Kernel: Remove unused mutex from xHCI
This was preventing multiple xHCI controllers to do parallel device
initialization
2024-11-22 22:18:38 +02:00
Bananymous 1bdabe693f Kernel: Preserve RsvdP bits in xHCI controller initialization 2024-11-22 22:18:09 +02:00
Bananymous ce09f35275 Kernel: Fix PCI 64 bit MSI msg_data offset 2024-11-22 22:17:25 +02:00
Bananymous 40c13043b3 Kernel: Add support for scratchpad buffers in xHCI 2024-11-22 22:16:57 +02:00
Bananymous e620068416 Kernel: Allow USB devices to handle STALL conditions 2024-11-22 22:15:22 +02:00
Bananymous 8e624ca85a Kernel: Don't panic if uninitialized AHCI device gets an interrupt 2024-11-21 20:41:13 +02:00
Bananymous c7afd46016 Kernel: Don't log spurious interrupts
Some PCs seem to be spamming a lot of int 7
2024-11-21 20:40:29 +02:00
Bananymous 3573656244 Kernel: Make PARTUUID matching case insensitive
Case sensitivity made it super annoying to work with :)
2024-11-21 20:39:27 +02:00
Bananymous 1de6de975a Kernel: Rename cmdline UUID= to PARTUUID=
This makes more sense and behaves more like existing software.
2024-11-21 20:38:50 +02:00
Bananymous 15c55b8c7d BuildSystem: Don't hardcode partition UUID
That was dumb :D I can just read the UUID parted assigned...
2024-11-21 20:37:42 +02:00
Bananymous 865061b492 Kernel: Temporary fix to make tmpfs more stable
whole TmpFS will have to be rewritten at some point :)
2024-11-21 18:13:26 +02:00
Bananymous 39313e1737 BuildSystem: Add option to boot using IDE or USB drives 2024-11-21 18:09:35 +02:00
Bananymous 70880636f4 Kernel: Implement basic USB Mass Storage support
I finally decided to do this :D
2024-11-21 18:08:37 +02:00
Bananymous 96a5ba0ed3 BuildSystem: Make root partition UUID fixed
This allows specifying root as UUID which makes booting nicer experience
on real hardware
2024-11-21 17:59:38 +02:00
Bananymous 8054f6c618 Kernel: Wait 10 seconds to find root device
If root device is initialized asynchronously it may not be ready when
main initialization thread is finished. This patch adds searching for
root device every 500 ms for 10 seconds.
2024-11-21 17:56:58 +02:00
Bananymous 658ea68d04 Kernel: Check max packet size for all USB devices
Spec says that this has to be done. Most cases can be probably skipped
as I used to do, but this is less error prone and adds practically no
overhead
2024-11-21 13:46:42 +02:00
Bananymous 5750f87396 Kernel: Fix unaligned access in xHCI controller initialization 2024-11-21 13:46:09 +02:00
Bananymous 1253e2a458 Kernel: Add support for bulk endpoints and update endpoint API
USB device now sets its own data buffers for IN/OUT endpoints. This
allows more customization and parallelism as data buffer does not have
to be shared.
2024-11-21 13:44:21 +02:00
Bananymous 857b3e92f8 Kernel: Support initializing USB 3 devices 2024-11-21 13:40:16 +02:00
Bananymous 8bf14d542e Kernel: Move SCSI device rdev allocation out of ATA code 2024-11-21 13:36:59 +02:00
Bananymous 31d2a39540 BAN: Add default constructors for BAN::{Little,Big}Endian 2024-11-21 13:34:12 +02:00
Bananymous 97718b4046 Kernel: Don't launch init process twice
I had accidentrally left the old init process launch in when replacing
it with the new one.
2024-11-19 20:43:38 +02:00
Bananymous c07fd265f0 Kernel: Add support for ATA CHS addressing and cleanup code
I thought that I had an PC without LBA support so I implement support
for CHS. Turns out that my ATA device detection was broken and was no
device on that port and initialize data was just garbage.

Now that I added CHS, I guess I should just keep it in :)

Both ATA read and write are now combined into a single function to avoid
code duplication.
2024-11-19 20:39:32 +02:00
Bananymous 1de50a2a94 Kernel: Improve ATA device initialization
This does better detection of empty ports and fixes floating bus
read-value from 0x00 to 0xFF.
2024-11-19 00:25:42 +02:00
Bananymous 627ca432cc Kernel: Make PS/2 controller not hang if device keeps sending resend 2024-11-19 00:18:48 +02:00
Bananymous f8ef11b4d2 BAN: Make PlacementNew.h only include <new> if it exists
Otherwise it will define the functions itself. This allows compiling
toolchain as stdlibc++ depends on BAN.
2024-11-19 00:16:44 +02:00
Bananymous 71563034a7 Kernel: Remove accidentally left debug prints 2024-11-18 03:45:07 +02:00
Bananymous aaff5a65e1 Kernel/init: /dev/tty is now custom symlink to controlling terminal
kernel now passes the name of default console to init process so init
knows which file to open as stdio. before /dev/tty was referencing the
system wide current terminal which was inherited from cmdline. This
doesn't work anymore as we have pseudo terminals implemented that can
chage the current terminal during runtime :D
2024-11-17 22:38:52 +02:00
Bananymous 458a362f98 Kernel: Allow kernel to pass arguments to init process 2024-11-17 22:38:02 +02:00
Bananymous 36d07065fb WindowServer: Optimize msync calls to synchronize less bytes
This *probably* makes framebuffer much faster :D Window server does not
have to do page aligned and page sized syncs which used way too many
pixels.
2024-11-17 20:15:28 +02:00
Bananymous f206e72447 Kernel: Don't require msync addresses to be page aligned
Inodes implementing msync can require page aligned addresses, but are
also allowed to not :) This reduces sizes of framebuffer msyncs!
2024-11-17 20:15:28 +02:00
Bananymous 58e45fb394 Kernel: Print process name on exceptions if it is available 2024-11-17 20:15:28 +02:00
Bananymous 411f32c766 WindowServer: Make select timeout more precise to hit target FPS
Previously window server was only running at around 40 FPS while
targetting 60. This makes the select timeout properly sleep so that
target FPS is reached!
2024-11-17 20:15:28 +02:00
Bananymous e1b82e4e43 WindowServer: Don't allow focusing unfocusable windows after closing 2024-11-17 20:15:28 +02:00
Bananymous df613775b6 WindowServer: Alpha blend background image in fullscreen mode 2024-11-17 20:15:28 +02:00
Bananymous 5e8fdc997a Kernel: Prevent userspace from overwriting CPU load memory 2024-11-17 20:15:28 +02:00
Bananymous 51bfe4252d ports/doom: download doom1.wad to common directory between archs 2024-11-15 20:50:40 +02:00
Bananymous c96aee5740 WindowServer: Make mod+f toggle focused window fullscreen state 2024-11-15 20:45:56 +02:00
Bananymous 1c08849667 WindowServer: Draw cursor in fullscreen mode 2024-11-15 20:45:01 +02:00
Bananymous 73349e5e75 WindowServer: don't set window location to (0,0) when fullscreening 2024-11-15 20:43:41 +02:00
Bananymous 74ae411b69 WindowServer: Skip invalidation on other windows in fullscreen mode 2024-11-15 20:42:07 +02:00
Bananymous d458592fb1 WindowServer: Don't focus unfocusable windows on window creation 2024-11-15 20:41:42 +02:00
Bananymous 627c89a62d LibGUI: Actually set window attributes when creating a window 2024-11-14 23:33:12 +02:00
Bananymous 0228cd4f31 Kernel: Increase userspace stack size and decrease kernel stack
Quake II needs a lot of stack, it was overflowing my 256 KiB stack so
this patch doubles that, so Quake II can run!

Also every thread had 256 KiB kernel stack. This is unnecessarily large
and now dropped to 32 KiB.
2024-11-14 21:03:04 +02:00
Bananymous a859558840 LibGUI: Window Creation takes attributes as an argument
This reduces windows showing/moving once they are opened and setting
their attributes.
2024-11-14 20:57:34 +02:00
Bananymous 04eee2ea75 ports: Add quake2 port (+two years of banan-os!)
This patch adds software rendered quake2 port. I was supposted to push
this as two days ago as two years of banan-os, but I was too sick to do
any programming.
2024-11-14 12:47:30 +02:00
Bananymous bda2c663da WindowServer: Implement fullscreen windows
If window size does not match framebuffer size, window data will be
scaled to framebuffer using nearest sampling for best performance.
2024-11-13 19:10:15 +02:00
Bananymous 5e041e6e5a WindowServer: Make all terminating signals exit cleanly
This allows window server to restore input handling to TTY if window
server crashes on page fault or some other unexpected exception.
2024-11-13 17:32:13 +02:00
Bananymous d19264eea8 WindowServer: Implement partial window resizing
This patch adds support for client side resizing, so clients can request
the server to resize their windows. WindowServer will respond with
resize event when and if the resizing is complete.
2024-11-13 17:30:12 +02:00
Bananymous 64c52012df Kernel: Fix MemoryRegion overlap comparison
This made some calls to `munmap` unmap extra regions.
2024-11-13 17:26:42 +02:00
Bananymous 7542e55cb2 LibC: Add option to debug userspace malloc family calls 2024-11-10 14:03:25 +02:00
Bananymous 6bd51ac345 LibGUI: Client window now exits if it loses connection from server
This can be prevented by installing callback for socket errors.
2024-11-10 14:02:23 +02:00
Bananymous cf21eb4b39 TaskBar: Cleanup TaskBar string generation 2024-11-08 02:57:35 +02:00
Bananymous da8170c5b6 LibGUI: Add support for focusable windows and mouse capturing
These are essential parts of a window server! This allows making TaskBar
non-focusable.
2024-11-08 02:55:10 +02:00
Bananymous 12bc7480e0 LibC: Implement _Exit
This just calls _exit as POSIX says they can be equivalent
2024-11-08 02:52:12 +02:00
Bananymous 92862fdf39 LibC: Implement nan{,f,l} as functions
One port was using function pointers to these functions so macros don't
work.
2024-11-08 02:49:21 +02:00
Bananymous 4417268ecc BAN: only include <new> in PlacementNew.h
I was getting annoying multiple definitions for placement new and my
build system requires that c++ libraries are compiled. I guess this is
the easiest and cleanest solution
2024-11-08 02:48:07 +02:00
Bananymous 346853ee55 BAN: Implement sincos to math
This function calculates both sin and cos fast (hopefully)
2024-11-04 17:42:01 +02:00
Bananymous a82f00cb70 BAN: Fix HashMap::{insert,emplace}_or_assign to actually assing value
I seemed to completely forgotten to do the assignment itself :D
2024-11-04 15:42:04 +02:00
Bananymous 1838ea5c30 Kernel: Fix pseudo terminal leaks 2024-11-04 15:35:07 +02:00
Bananymous a356d90445 LibC: Fix libc compilation for 32 bit target 2024-11-04 10:51:31 +02:00
Bananymous a3f41fd4d9 Kernel: Cleanup disk cache
Add missing include and cleanup code calling automatic disk sync.
2024-11-03 20:28:15 +02:00
Bananymous f4be37700f Kernel/userspace: rework floating point math
SSE is now unconditionally enabled any where and most of math.h is now
actually implemented. using __builtin_<func> lead to many hangs where
the builtin function would just call itself.
2024-11-03 20:28:15 +02:00
Bananymous ed19bb11fe Kernel: Cleanup thread default sse initialization 2024-11-03 20:22:28 +02:00
Bananymous 07275ecb5f BAN: Implement floating point constants in Numbers.h 2024-11-03 20:19:45 +02:00
Bananymous 2464fccadd LibC: Fix *scanf consuming extra characters 2024-10-19 12:43:23 +03:00
Bananymous 5fde2cf91a TaskBar: Ignore window close events and exit on connection close
This prevents user from killing the task bar with keyboard shortcuts
2024-10-18 17:19:05 +03:00
Bananymous d57e797147 WindowServer: Don't kill window on key event, instead send close event
This allows notifying the client about window close and let client
handle it as it wants
2024-10-18 17:17:58 +03:00
Bananymous 988f7b0561 ports: Fix doom port to use the new GUI events 2024-10-18 03:45:15 +03:00
Bananymous e99a271465 Userspace: Implement basic task bar
This bar only shows current time but its something to work on later!
2024-10-18 03:36:03 +03:00
Bananymous d266c7f93b LibGUI: Implement attributes for windows
Windows can now change whether they have title bar, rounded corners,
alpha channel and whether they are movable. Also windows can also change
their own position
2024-10-18 03:32:12 +03:00
Bananymous d7e5c56e94 userspace: Use SOCK_STREAM instead of SOCK_SEQPACKET for WindowServer
This makes more sense if we have longer packages
2024-10-17 01:44:48 +03:00
Bananymous ddd3b4c093 BAN: Implement is_pod type traits 2024-10-17 01:35:42 +03:00
Bananymous 3a6fc4c197 BAN: Add proper error messages for `MUST()` in userspace 2024-10-17 01:32:55 +03:00
Bananymous 91381546d5 Shell: Don't crash if history file does not exist :D 2024-10-16 00:41:28 +03:00
Bananymous 41e2b898ab Shell: Implement history file for persistent history storage 2024-10-16 00:21:58 +03:00
Bananymous 6e981d1222 Shell: Add support for inline environment variables
e.g. `foo=$(echo lol) Shell -c 'echo $foo'` will now print lol!
2024-10-15 23:45:08 +03:00
Bananymous 8317bb13ca Shell: Cleanup code by defining argument types as nested types 2024-10-15 23:44:06 +03:00
Bananymous c40f244dff Shell: remove `env` builtin and add `type`
other shells don't seem to implement as a builtin, so i won't either
2024-10-15 23:42:01 +03:00
Bananymous a6aa048be0 userspace: Implement `env` as an executable 2024-10-15 23:42:01 +03:00
Bananymous 8fd0162393 Kernel: Rewrite x86_64 page tables to use HHDM instead of kmalloc
This allows page tables to not crash the kernel once kmalloc runs out of
its (limited) static memory.
2024-10-14 11:40:30 +03:00
Bananymous f0b18da881 Kernel: Add kmalloc helper APIs 2024-10-14 11:39:04 +03:00
Bananymous 5f63ea8f8a Kernel: Add CPUID check for 1 GiB page support 2024-10-14 11:38:03 +03:00
Bananymous 2b43569927 Kernel: Use enums in boot info instead of magic values 2024-10-14 11:36:51 +03:00
Bananymous 4ba33175cf Kernel: Don't leak memory when preparing fast page
For some reason I was allocating memory for page table entry...
2024-10-14 11:34:48 +03:00
Bananymous 3edc1af560 Kernel: Don't map main bios area in page table initialization
This is only needed for RSDP lookup so it can be done with fast pages
2024-10-14 11:32:54 +03:00
Bananymous 55fbd09e45 Kernel: Rewrite physical memory allocation with PageTable::fast_pages 2024-10-14 11:32:54 +03:00
Bananymous 6a46a25f48 image: Add benchmark flag to measure performance of image operations 2024-10-13 22:05:13 +03:00
Bananymous 88b8ca5b29 LibC: Fix some string functions
I was not casting some required values to char or handling length of
zero
2024-10-13 22:04:08 +03:00
Bananymous fdddb556ae LibC: Implement system() more properly
Old implementation did not ignore and block needed signals
2024-10-13 22:03:15 +03:00
Bananymous d36b64e0c8 LibImage: name color to u32 function to to_argb from to_rgba
This is the actual format that it returns
2024-10-13 22:01:46 +03:00
Bananymous 8adc97980a Shell: rewrite the whole shell to use tokens instead of raw strings
tab completion is still running with raw strings and that has to be
fixed in the future.
2024-10-13 22:00:16 +03:00
Bananymous dab6e5a60f BAN: Cleanup HashMap implementation and add {insert,emplace}_or_assign 2024-10-13 22:00:16 +03:00
Bananymous 0b05e9827b BAN: Use memmove instead of memcpy on overlapping data
I was accidentally using memcpy where memmove was needed
2024-10-13 22:00:16 +03:00
Bananymous 1c1a76d6d7 BAN: Member function pointers now use references instead of pointers
This seems cleaner as class pointer cannot be null anymore
2024-10-13 22:00:16 +03:00
Bananymous df4f37d68d BAN: only define placement new operators for banan-os targets
This allows building and using BAN library outside of banan-os!
2024-10-10 21:55:25 +03:00
Bananymous 44629ba5dd BAN: Allow userspace to use string literals with BAN::Error 2024-10-10 21:54:52 +03:00
Bananymous 2da6776451 BAN: Update {Byte}Span API with better constness
const BAN::Span<int> is now allowed to modify its underlying data, but
the container itself is const.

BAN::Span<const int> can be used for spans over constant data.
2024-10-10 21:53:23 +03:00
Bananymous a68f411024 BAN: Add requires clauses for Container::emplace{,_back} functions
This allows syntax highlighters to report errors!
2024-10-10 21:51:44 +03:00
Bananymous 166550fbba Kernel: Ext2 check if file exists before creating it 2024-10-07 18:21:05 +03:00
Bananymous 3e68981b0b userspace: Remove start-gui from shell builtins and make it an alias
This makes way more sense :D
2024-10-07 18:01:33 +03:00
Bananymous 6fd76e8d1e Shell: Rewrite alias parsing to allow chained commands
You can now have "chained" commands in alias
e.g. `alias foo='echo hello && ls`
2024-10-07 18:01:33 +03:00
Bananymous dfcd15e7c4 Shell: Implement background processes with & 2024-10-07 18:01:33 +03:00
Bananymous 5fa359c28d ls: Print file name in quotes if it contains spaces 2024-10-07 18:01:32 +03:00
Bananymous 5bcfc9dd50 Shell: Always print prompt to new line
This is only done on non banan-os targets if terminal is detected to
support cursor position querying
2024-10-07 18:01:32 +03:00
Bananymous f67cad326a Shell: Don't crash when there are no tab completions available :D 2024-10-07 14:26:07 +03:00
Bananymous 9775e83374 Shell: Fix tab space escaping in tab completion 2024-10-07 04:19:36 +03:00
Bananymous 264d1798dc Shell: Clenup code and fix some bugs
Don't list tab completion multiple times, allow `clear` to work even
when ANSI CSI 3K is not supported, reset buffer index when cancelling
command with ctrl+c
2024-10-07 04:10:25 +03:00
Bananymous 1824988b9a Shell: Add support for simple aliases
Aliases do not support chained commands with pipes, &&, ... but this is
a good start.
2024-10-07 04:09:38 +03:00
Bananymous c54d9b3f60 Shell: Implement simple tab completion for commands and files 2024-10-07 01:44:44 +03:00
Bananymous f432d3fcf8 BAN: Cleanup Optional casting for value getters 2024-10-07 01:44:44 +03:00
Bananymous 4f7828bab9 Shell: Allow escaping spaces in commands 2024-10-07 01:44:44 +03:00
Bananymous ae073a336d Shell: Move builtin commands to a hash map
This allows accessing builtin commands outside of `execute_builtin`
2024-10-07 01:44:44 +03:00
Bananymous 6f90974896 BAN: Fix StringView::starts_with
I have no idea what i had been thinking when writing this code :D
2024-10-06 06:23:25 +03:00
Bananymous 4f3c05851c Shell: Expand ~ to home if its at the start of non quoted argument 2024-10-06 01:38:43 +03:00
Bananymous 4b13055125 Shell: Make builtin exit command read exit value from argument
Shell used to unconditionally return with exit code of 0
2024-10-06 01:24:34 +03:00
Bananymous d542cd811d Shell: Add support for &&, || and ; 2024-10-06 01:24:24 +03:00
Bananymous f75cebac7e Shell: Mark all functions as static 2024-10-05 19:11:58 +03:00
Bananymous e302b6b635 Shell: Use sigaction instead of signal
This allows ctrl-c to work on linux :D
2024-10-05 19:07:59 +03:00
Bananymous f709e88994 Shell: Cleanup argument handling and exiting 2024-10-04 17:54:01 +03:00
Bananymous ab9a6d583b Userspace: Set SHELL environment variable in init instead of shell 2024-10-04 17:24:37 +03:00
Bananymous 26d6bf338e Shell: Ignore SIGTTOU and set pgroup from parent instead of child
This allows using the shell in linux!
2024-10-04 17:23:31 +03:00
Bananymous b6e040dfc2 Kernel/Shell: Fix backspace and delete key byte sequences 2024-10-04 04:14:47 +03:00
Bananymous f5802ca3e5 General: Update screenshot in README 2024-09-30 18:28:16 +03:00
Bananymous 7223e581a2 WindowServer: Fix 32 bit compilation with -Werror
There was a always false statement on 32 bit that the compliler was
warning about.
2024-09-27 15:33:25 +03:00
Bananymous 2d11ce9669 Kernel: Fix interrupt system
I had not understood how MSIs work and I was unnecessarily routing them
through IOAPIC. This is not necessary and should not be done :D

Also MSIs were reserving interrupts that IOAPIC was capable of
generating. Now IOAPIC and MSIs use different set of interrupts so
IOAPIC can use more interrupts if needed.
2024-09-27 15:31:31 +03:00
Bananymous e4982a1a5c LibC: Fix printf with alternate format and zero values 2024-09-26 15:25:16 +03:00
Bananymous cea6dedccc Userspace: Compile programs and libraries with -Wall -Wextra -Werror 2024-09-26 15:20:07 +03:00
Bananymous e6ed5a388d BuildSystem: Export compile commands for clangd 2024-09-26 15:14:54 +03:00
Bananymous b89fc3fe87 Kernel: Implement ANSI SGR 7 to invert colors
This allows vim's visual selection to show up
2024-09-26 15:08:11 +03:00
Bananymous 57ae74f908 Terminal: Implement more ANSI escape handling
This patch adds L and M codes for inserting and deleting lines and SGR 7
for inverting colors
2024-09-26 15:07:08 +03:00
Bananymous 1a6804b4b4 Terminal: Make Terminal::handle_csi() return invalidated rectangle
When I updated Terminal to only do a single invalidation after all of
input text was printed, I forgot to change handle_csi
2024-09-26 15:05:11 +03:00
Bananymous 82e6a3582d LibGUI: cleanup Window::shift_vertical and add copy_horizontal_slice 2024-09-26 15:00:14 +03:00
Bananymous 11a4e4faa2 LibImage: Add Adam7 support for PNG decoder 2024-09-26 11:47:34 +03:00
Bananymous 50a3533322 LibInput: Add value for ESC in key_to_utf8_ansi
This allows exiting vim when using this function for key mappings :D
2024-09-24 18:08:34 +03:00
Bananymous 4e9f39384a LibC: Initialize libc only once
This was breaking stuff with dynamic loader initializing libc and main
executable reinitializing it later :D
2024-09-24 17:56:12 +03:00
Bananymous 39802b56c1 Kernel: Allow SYS_EXEC to fail at any point
This patch builds new executable image to another pml4 structure and
only after everything is validated will current context be replaced.
This allows exec to fail "late" where previously it would panic the
kernel or kill the process. This allows graceful handling of exec
failures in userspace!
2024-09-24 16:29:38 +03:00
Bananymous ebb87ccdde Kernel: Add asserts for overflow checks with ms -> ns conversions 2024-09-24 16:28:44 +03:00
Bananymous f0e55938c1 Kernel: Allow initial binding of threads to specific processor 2024-09-24 16:27:40 +03:00
Bananymous 348d04f7f5 Kernel: Implement static Process::kill()
This allows killing processes even when there does not exist a current
thread.
2024-09-24 13:16:43 +03:00
Bananymous 1b0086217c Terminal: Do scrolling in putchar if necessary
I overlooked that handling *newline* scrolls would be enough. You can
definitely scroll more if printing a lot of text which wraps to the next
line.
2024-09-23 15:00:47 +03:00
Bananymous d395cf38b7 Kernel: Binding to port 0 will always find unused port 2024-09-22 17:30:01 +03:00
Bananymous 57aec2a733 Kernel: Validate pointer's when printing stack trace
There was an kernel panic if stack trace contained uncanonical
addresses.
2024-09-22 17:16:01 +03:00
Bananymous ae89237453 Kernel: Disable warning when calling syscall
This warning is only generated when compiling with UBSAN and I don't see
how the value would even be uninitialzed.
2024-09-22 17:15:07 +03:00
Bananymous 60d5257678 Kenrel: Add __builtin_unreachable support for UBSAN 2024-09-22 17:14:36 +03:00
Bananymous d59463d11b Kernel: Fix TTY reading one keyevent after disabling input handling 2024-09-22 17:13:10 +03:00
Bananymous 1280528e4e BuildSystem: Use -O2 for all libraries
I don't know why I had not specified optimization level for libraries.
Only LibImage was using explicit -O3. LibImage doesn't need O3 anymore
as O2 seems to be around the same speed.
2024-09-20 20:04:49 +03:00
Bananymous 23d6205659 Kernel: Move DEBUG_* macros to centralized Debug.h
This makes toggling debug info much easier
2024-09-20 11:00:07 +03:00
Bananymous bc0acc6f44 Kernel: Validate network packet sizes before casting
This caused a lot of crashes on invalid packets :D
2024-09-20 10:46:59 +03:00
Bananymous b8622f2b4b Kernel: Implement simple RTL8169 driver
This allows me to use internet when running banan-os on my hardware!
2024-09-19 22:58:03 +03:00
Bananymous 7f0c39fe91 Kernel: Don't crash the kernel if packet is smaller than ethernet hdr 2024-09-19 22:51:55 +03:00
Bananymous a489be0e05 Kernel: Allow parallel LAPIC timer initialization with HPET
HPET supports reading LAPIC counter without locks, so it can be done in
parallel. This makes booting much faster. Previously initializing every
timer took 100 ms, so 16 CPUs took total of 1.6 seconds. This allows
doing it all in 100 ms.
2024-09-19 14:41:59 +03:00
Bananymous 8e08046519 Kernel: Add asserts about having locked TTY's write lock
This for some reason fixes booting on real hardware? :D
2024-09-18 00:56:48 +03:00
Bananymous 999eb53364 Kernel: Release debug lock if its locked during kernel panic
Also kernel panic now prints if it had lock
2024-09-18 00:56:02 +03:00
Bananymous f7e549e412 Toolchain: Run `strip` after installing toolchain
This drops installed toolchain size to around fifth :D
2024-09-17 22:30:40 +03:00
Bananymous fbb99de728 ls: Show symlink target with `-l` 2024-09-17 19:12:34 +03:00
Bananymous 0620ed3d4f Kernel: Fix file open permissions for some syscalls 2024-09-17 19:11:48 +03:00
Bananymous b779b3cf2d Kernel: Move file finding code to a helper 2024-09-17 18:52:52 +03:00
Bananymous a6b973003b WindowServer: chmod the correct file 2024-09-17 18:35:27 +03:00
Bananymous e431e90b20 Kernel/LibC: Implement all chown family function with fchownat 2024-09-17 18:35:01 +03:00
Bananymous 4aa466b948 Kernel/LibC: Implement all chmod family functions using fchmodat 2024-09-17 17:19:26 +03:00
Bananymous 04ae53b6df Shell: Add back accidentally deleted `cd` and `time` builtins 2024-09-17 16:38:45 +03:00
Bananymous 3666525d24 Kernel/LibC: Implement `readlink` in terms of `readlinkat` 2024-09-17 16:38:15 +03:00
Bananymous f1a4bbce53 Kernel/LibC: Implement all stat family functions with fstatat
This patch gets rid of 2 unnecessary syscalls!
2024-09-17 16:38:01 +03:00
Bananymous 708a720d9d Kernel: Remove SYS_OPEN
This can be done with SYS_OPENAT with fd set to AT_FDCWD
2024-09-17 16:16:47 +03:00
Bananymous 7177da7d62 LibC: Implement dummy get_rusage
One port seems to use this function. This dummy just reports no used CPU
time for current process nor children
2024-09-17 15:59:22 +03:00
Bananymous 5e4aa75e03 Kernel: Perform access checks when creating a file or a directory
Also SYS_CREATE_DIR now uses correct relative path. It used to always
create files relative to root. And as no permission checks were tested,
file creation succeeded to root as long as path did not contain '/' :D
2024-09-17 15:57:07 +03:00
Bananymous d88ee5c9ee Kernel/LibC: Implement `creat` with `open`
This allows getting rid of unnecessary SYS_CREATE. Directory creation
still has its own syscall, but I could combine it with SYS_OPEN also.
2024-09-17 15:55:53 +03:00
Bananymous d4ea720239 Kernel: Don't crash the kernel if ext2 encounters disk error
This will most likely result in a corrupted filesystem, but crashing the
kernel is too much :D
2024-09-17 15:54:33 +03:00
Bananymous 97ee370ffe Kernel: Allow listing files even if one of directories blocks are empty 2024-09-17 15:53:14 +03:00
Bananymous a084f83f4c Kernel: Rework kernel-side ELF loading
ELFs are now loaded as MemoryRegions so they don't need special handling
anywhere. This also allows file backed COW optimizations to work. This
was not the case before.

This patch removes now obsolete LoadableELF and unused ELF files from
LibElf.
2024-09-15 23:57:34 +03:00
Bananymous 54732edff4 Kernel: Fix bugs in memory regions
MemoryRegion::initialize() required size to be page aligned, this is not
necessary.

MemoryBackedRegion::copy_data_to_region() required user-write
permissions for underlying data. This did not matter as memory backed
regions dont support COW memory, but it could lead to bugs later on.
2024-09-15 23:18:05 +03:00
Bananymous 1a24d1839f LibInput: KeyEvent to ansi string now handles delete key
This allows Terminal emulator to also handle delete key.

The value of `\0x7F` may be wrong as qemu's serial console seems to be
sending `\e[3~`
2024-09-15 03:13:21 +03:00
Bananymous c3040a04a3 Terminal: Optimize printing a lot
Terminal used to run `yes` at around 400 lines per second

This patch pumps that to over 100'000 lines per second!

There are 2 optimizations done:
  1. only invalidate window once after rendering is done
  2. if printing more than `rows()` newlines skip prior data
2024-09-15 03:12:26 +03:00
Bananymous 7feb4c4ebd Kernel: VFS::file_from_absolute_path now used root_file() API 2024-09-15 02:38:25 +03:00
Bananymous 2911d1f018 Kernel: Cleanup and fix pseudo terminals 2024-09-15 02:38:07 +03:00
Bananymous d68ad893f0 Kernel/Shell: Add support for delete key 2024-09-14 22:45:48 +03:00
Bananymous 4ca147699d Shell: use printf and putchar instead of fprintf and fputc 2024-09-14 22:37:09 +03:00
Bananymous abed41b8fa Shell: Remove builtin tests
Test should be programs in /bin instead
2024-09-14 22:28:33 +03:00
Bananymous 2604a55f80 Shell: Make ctrl+c start a new command 2024-09-14 22:27:32 +03:00
Bananymous 39667de662 Kernel: Fix sizeof check for sys_tc{set,get}attr 2024-09-14 21:17:03 +03:00
Bananymous 8956835d95 Kernel: Make sys_.*at use VFS relative path finding 2024-09-14 21:15:58 +03:00
Bananymous ea4ec2eafc Kernel: Make OpenFileDescrptor store VFS::File instead of inode+path 2024-09-14 20:10:21 +03:00
Bananymous 3a352078de Kernel: Make VirtualFileSystem::File non copyable 2024-09-14 20:09:28 +03:00
Bananymous 6060b39548 Kernel: Implement relative file searching in VFS 2024-09-14 19:44:20 +03:00
Bananymous dce2436b2c BAN: Implement Vector::reverse() 2024-09-14 19:44:20 +03:00
Bananymous 2b52ea4c6f Kernel: Make Inode::can_access const 2024-09-14 19:39:41 +03:00
Bananymous b41738b47b Kernel: Fix ACPI name formatting functions 2024-09-14 19:39:13 +03:00
Bananymous 57e76a65a4 Kernel: Null terminate ext2 directory entries
This fixes a weird bug with `ls /bin`
2024-09-12 23:55:34 +03:00
Bananymous 7f25ddc229 userspace: Add missing alpha channels 2024-09-12 20:53:45 +03:00
Bananymous 163961df4f ports/doom: Add alpha channel for framebuffer access 2024-09-12 20:16:17 +03:00
Bananymous 97da386ed6 WindowServer: Make focused window bounce by pressing F2
This was an awesome idea by [@Dcraftbg](https://github.com/Dcraftbg)
2024-09-12 20:09:23 +03:00
Bananymous c706829a91 WindowServer: Make corner-radius a variable in config file! 2024-09-12 20:07:30 +03:00
Bananymous 6cd246a38e WindowServer: Add support for rounded corners and transparency 2024-09-12 19:35:44 +03:00
Bananymous 43e88c0ae0 Terminal: Add alpha channel to colors
This doesn't do anything yet, but will when I add transparency support
for WindowServer
2024-09-12 19:34:25 +03:00
Bananymous bf01b935bd WindowServer: Start Terminal with Super+Enter
this allows doing something in GUI after Terminal is closed

also WindowServer is now stopped with Super+Shift+E
2024-09-11 22:18:08 +03:00
Bananymous 98c011e6a6 WindowServer: Open all fds as CLOEXEC
This will allow forking the window server cleanly :D
2024-09-11 22:17:31 +03:00
Bananymous 86dcb5c471 init: Don't leak pwent fd to child processes 2024-09-11 22:11:05 +03:00
Bananymous 467ac6c365 Kernel/LibC: Implement SOCK_CLOEXEC and SOCK_NONBLOCK
This removes the need for fcntl after creating a socket :)
2024-09-11 21:59:11 +03:00
Bananymous c77ad5fb34 Kernel: Implement copy-on-write memory for file backed `mmap`s 2024-09-11 19:33:50 +03:00
Bananymous 4006a04817 DynamicLoader: Don't do file backed mapping over file bounds 2024-09-11 19:30:35 +03:00
Bananymous 4189a1c729 LibC: Make _init and _fini weak symbols
These seem to be missing sometimes when making shared executables
2024-09-11 14:39:07 +03:00
Bananymous a07cbabcb3 LibC: Define function sizes for setjmp and longjmp
dynamic linking was complaining about these not existing
2024-09-10 16:21:20 +03:00
Bananymous 64a3893f6f Kernel: Add command line option to disable AML parsing
This can be handy if my broken AML parser crashes or hangs while parsing
AML :D
2024-09-10 16:20:24 +03:00
Bananymous eabe759ebf Kernel: Don't require APs that are not started to start :D 2024-09-10 16:19:43 +03:00
Bananymous a4838386e6 Kernel: Remove unnecessary branch from uncanonicalizing addresses 2024-09-10 16:19:16 +03:00
Bananymous c65613901f Kernel: Fix AML aliases and package elements 2024-09-10 16:18:42 +03:00
Bananymous 19d16620a6 DynamicLoader: Don't do file backed mapping for filesz == 0 2024-09-05 14:50:05 +03:00
Bananymous 4e15b9cdfa Ports: Add explicit -libgcc-shared for ports on 32 bit 2024-09-05 14:49:23 +03:00
Bananymous de35cec2e1 Kernel: Allow private file mappings past file end
This is just to make memory mapping ELF files easier :D
2024-09-05 14:48:42 +03:00
Bananymous e8bcebfb8e DynamicLoader: Use file backed mmap when possible 2024-09-05 13:59:39 +03:00
Bananymous 2f241e1b61 Kernel: Implement FileBackedRegion::clone 2024-09-05 13:59:09 +03:00
Bananymous 6affef76b1 test-shared: Add more shared tests 2024-09-05 12:59:48 +03:00
Bananymous 32ba4d07e2 Userspace: Remove unnecessary mov instruction from crt0 2024-09-05 12:59:05 +03:00
Bananymous ddaaf89c87 BuildSystem: Make default libc a shared library 2024-09-05 12:58:35 +03:00
Bananymous 9aed8dbe6b Toolchain: Build crtbeginS.o crtendS.o and shared libgcc 2024-09-05 12:58:21 +03:00
Bananymous aa7e92b275 Userspace: Write a dynamic loader
This allows running executing dynamically linked executables!
2024-09-05 12:58:21 +03:00
Bananymous f30947336a BuildSystem: Cleanup cmake files 2024-09-05 12:52:25 +03:00
Bananymous 0bb44d7b75 LibC: Fix errno to work with shared libraries 2024-09-04 21:16:04 +03:00
Bananymous 1138ec77ca Kernel: Fix MemoryBackedRegion cloning for non page aligned size 2024-09-04 21:14:26 +03:00
Bananymous c4dadd27ac BAN: Fix refenrece -> reference typo 2024-09-03 16:39:21 +03:00
Bananymous 04c715d314 BuildSystem: Build all libraries in root build directory
I could not figure out how to use cmake's rpath to build libraries with
DT_NEEDED only containing the library name, not a relative path.
2024-09-02 21:31:08 +03:00
Bananymous 35743cc8e6 LibC: Compile both static and shared version of LibC
libc target is now set as an alias for libc-static
2024-09-02 21:28:02 +03:00
Bananymous 5f92807fdd userspace: Allow building without SSE
I had added changes that had broken compilation without sse support
2024-09-02 21:25:00 +03:00
Bananymous 700c3444f5 LibELF: Add more values and macros for interpreting fields 2024-09-02 20:40:38 +03:00
Bananymous ddf1babfe1 Kernel: Cleanup constructor calls 2024-08-31 02:23:08 +03:00
Bananymous 2ae2ede0b2 LibELF: Add value definitions for DT_ 2024-08-30 15:35:01 +03:00
Bananymous ef5af384e7 Kernel: Print userspace invalid pointer access address 2024-08-30 15:34:37 +03:00
Bananymous a134d16070 Kernel: Fix MemoryRegion test for contains_fully 2024-08-30 15:33:58 +03:00
Bananymous 827eec6af0 Kernel: Make .rodata read-only and add -orphan-handling=error
I don't really know why .rodata was marked writable :D
2024-08-30 15:33:02 +03:00
Bananymous 8da2f12ba6 Kernel: Only load program headers of interpreter if its present
I was loading program headers of both executable and interpreter but
that is incorrect. The interpreter will itself load the program headers
of the executable.
2024-08-28 21:19:37 +03:00
Bananymous 50ab391133 test-globals: print something when calling main
This shows that constructors and destructors are called in correct order
2024-08-28 17:09:07 +03:00
Bananymous 991647bc8f Userspace: Add shared library test
I will use this while testing my dynamic loader
2024-08-28 17:08:37 +03:00
Bananymous 57300687ff LibC: Cleanup and fix missing LibC headers 2024-08-28 17:07:58 +03:00
Bananymous d559339f5f LibC: Compile shared libc as well as static 2024-08-28 17:07:15 +03:00
Bananymous 2bf65ef512 Kernel: Invoke ELF interpreter instead if it is specified 2024-08-28 17:06:32 +03:00
Bananymous d20752c318 Kernel: Make OpenFileDescritorSet::open take rvalue
This gets rid of some implicit allocations from copy constructors
2024-08-28 16:36:10 +03:00
Bananymous 5f66ef34dd Kernel: map userspace arguments after the entry point
This allows cleaner memory layout for processes that are not loaded to
default location
2024-08-27 22:46:08 +03:00
Bananymous da0b4cd40e Kernel: Allow MAP_FIXED in mmap 2024-08-27 22:45:49 +03:00
Bananymous 68f9dc1f8a Toolchain: Add support for building shared libraries 2024-08-27 17:36:09 +03:00
Bananymous 5121d0d934 Kernel: Allow loading PIEs
This is initial work towards implementing dynamic loader and shared
library support.
2024-08-27 17:32:22 +03:00
Bananymous 2c520391eb Kernel/LibC: Add support for init_array and fini_array 2024-08-27 00:47:03 +03:00
Bananymous 7c4b9218f2 Kernel: VirtualTTY now resets ansi state before printing anything 2024-08-25 17:56:06 +03:00
Bananymous 72f8138ca1 Kernel: Disable scheduler load balancing until I get it fixed
Scheduler keeps crashing all the time when running on multiple cores.
This patch disabled the load balancer, which seems to get rid of most
scheduler crashes.
2024-08-25 15:37:17 +03:00
Bananymous 991ae4383a Kernel/LibC: Implement fchmod 2024-08-25 15:07:42 +03:00
Bananymous 2ce7205c80 Kernel: Add command line option to disable debug printing
The whole system can crash when debug output and tty output are done at
the same time. This patch is just a hack to prevent the crash :D
2024-08-25 15:02:15 +03:00
Bananymous bec3e8654f Shell: Implement escaping quotes in quoted strings 2024-08-24 17:13:50 +03:00
Bananymous 14fdcb892d Ports: Make curl port use openssl
Curl now works with https!
2024-08-22 15:10:41 +03:00
Bananymous d6d062841d Ports: Add openssl port 2024-08-22 15:10:41 +03:00
Bananymous 06d0985bba Ports: Add zlib port 2024-08-22 15:10:41 +03:00
Bananymous 9ea969be1f Ports: Add port that downloads and install cacert to system 2024-08-22 15:10:41 +03:00
Bananymous 500f774b7f LibC: Add a lot of stub implementations for different libc functions
some ports want to link against these, but they are not ever called
based on simple testing

This patch adds stubs for
- openlog
- syslog
- getrusage
- mlock
- mprotect
- getpeername
- shutdown
- tzset
- mktime
2024-08-22 15:10:41 +03:00
Bananymous ca8832c0e1 LibC: Implement getaddrinfo and freeaddrinfo, add stub for getnameinfo 2024-08-22 15:10:41 +03:00
Bananymous 1cbba113fd LibC: Add and cleanup some networking structures and definitions 2024-08-22 15:10:41 +03:00
Bananymous 45b4b33a3d LibC: Add definitions for some integer sizes to limits.h 2024-08-22 15:10:41 +03:00
Bananymous c453a8e2dc LibC: Implement poll() using select 2024-08-22 15:10:41 +03:00
Bananymous e59772a58d General: Cleanup build scripts 2024-08-22 15:10:41 +03:00
Bananymous fb35f06cf5 Kernel: Add better support for bootloaders loading the kernel
Before I assumed that bootloaders loaded the kernel at physical address
0, but this patch kinda allows loading to different addresses. This
still doesn't fully work as kernel bootstrap paging relies on kernel
being loaded at 0
2024-08-22 14:48:21 +03:00
Bananymous abc788c756 Kernel: Improve output message when dumping all syscalls 2024-08-22 14:48:21 +03:00
Bananymous 63b616dc2e Kernel: Hack non-blocking support for sockets
This is not thread safe and can still block if two programs refering to
the same socket try to read data at the same time
2024-08-22 14:48:14 +03:00
Bananymous 72f3fe0b12 LibC: Add empty files sys/param.h and sys/file.h
For some reason some ports include these files even when they detect
that they are not available
2024-08-22 14:47:59 +03:00
Bananymous cba12efeb1 Kernel: Ignore optional_actions in SYS_TCSETATTR
Currently all terminals are syncronous, so the actions don't do anything
2024-08-22 14:47:54 +03:00
Bananymous 37cd4ed504 Kernel: Add support for CSI @, b, d to VirtualTerminal 2024-08-22 14:04:45 +03:00
Bananymous 869bba4dad Kernel: Add check for panic in timer interrupt handler
Previously processors would not get notified about kernel panic if they
were idling
2024-08-22 14:03:03 +03:00
Bananymous 23194d1f53 Kernel: Implement basic random device to /dev/random 2024-08-22 14:02:26 +03:00
Bananymous 969563c06a Kernel: Don't load AP init code to 0xF000, but relocate it later
This cleans up the kernel executable as bootloaders don't have to
load AP init code straight to 0xF000, but it will be moved there once
kernel is doing the AP initialization.
2024-08-21 13:37:50 +03:00
Bananymous 066e8e1cc2 Kernel: Remove unnecessary debug logging from VFS initialization 2024-08-18 20:51:10 +03:00
Bananymous 5cd7b40165 Kernel: Make BananBootloader info struct header compatible with C 2024-08-18 20:51:10 +03:00
Bananymous c7b134ba4b Kernel: Fix NVMe controller namespace numbering
namespace numbers were incrementing globally instead of per controller.
This led to two single namespace controllers creating nvme0n1 and
nvme1n2
2024-08-18 20:51:10 +03:00
Bananymous ff62c262fe Kernel: Fix PS/2 scancode set 2 keycodes for the bottom row keys 2024-08-18 20:51:10 +03:00
Bananymous 42e2c15e0c Kernel: Add ps2=<scancode set> command line argument
This allows forcing specific scancode set on broken PS/2 emulations
2024-08-18 20:51:10 +03:00
Bananymous 40c6989374 Kernel: Implement AML ObjectTypeOp 2024-08-18 20:51:10 +03:00
Bananymous 71dc373610 Kernel: "Fix" AML _OSI string to return true for windows strings
This is the way its supposed to be done as other code paths are
untested...
2024-08-18 20:51:10 +03:00
Bananymous 0fa16cf982 Kernel: Fix and add some AML to_underlying functions 2024-08-18 20:44:51 +03:00
Bananymous 8902032b42 BuildSystem: Cleanup kernel cmake file 2024-08-18 20:44:12 +03:00
Bananymous 368f5e9799 Kernel: Add command lineoption `nousb` that will disable usb controller 2024-08-16 22:09:24 +03:00
Bananymous 46b34817d2 snake: Remove random goto statements when regenerating apple's position 2024-08-16 17:36:37 +03:00
Bananymous b1fe24bb57 Kernel: Hack AML integers to work better
Something is trying to store into a constant integers. Just by copying
not returning any integers as constants fixes it xD
2024-08-16 13:04:52 +03:00
Bananymous 490a28ee7a Kernel/AML: General cleanup and compliance improvements 2024-08-15 23:14:13 +03:00
Bananymous 75884ca6b8 Kernel/AML: Allow calling method through named objects 2024-08-15 23:13:32 +03:00
Bananymous d729d7f570 Kernel: Implement AML storing to Buffer 2024-08-15 23:12:52 +03:00
Bananymous 6408bb2efa Kernel: Add AML API for getting underlying value of nodes 2024-08-15 23:11:59 +03:00
Bananymous d8dabab4fb Kernel: Implement AML CopyObjectOp 2024-08-15 20:55:55 +03:00
Bananymous 4dc107f77a Kernel: Fix AML Register/Reference accesses 2024-08-15 20:55:18 +03:00
Bananymous eaf06d239c Kernel: Cleanup and fix AML Method calls 2024-08-15 20:54:55 +03:00
Bananymous d9b3a4bf77 Kernel: Add more conversions for AML Integer, Register, String 2024-08-15 20:51:04 +03:00
Bananymous cf970d5914 Kernel: Add better conversion for AML Alias, Name and Reference 2024-08-15 20:48:35 +03:00
Bananymous 1cfe3dd4da Kernel: Implement AML BreakOp and ContinueOp 2024-08-15 19:08:00 +03:00
Bananymous 51d1e47bfe Kernel: Fix AML CreateFieldOp to take bit index instead of byte index 2024-08-15 19:05:58 +03:00
Bananymous e0a447bfaf Kernel: Add _GL to ACPI root namespace 2024-08-15 02:28:22 +03:00
Bananymous 3f5ee6f414 Kernel: Rewrite the AML conversion API
This doesn't currently make the interpreter any better, but it will make
further implementation easier to be spec (or hardware...) compliant
2024-08-15 02:25:45 +03:00
Bananymous 44d5c8c4b4 Kernel: Implement AML To{Buffer,Integer,String}Op 2024-08-14 20:28:32 +03:00
Bananymous 17b7e9e772 Kernel: Allow all named objects to fail cleanly if name exists 2024-08-14 20:28:00 +03:00
Bananymous 8a2a444f33 BAN: Make formatting functions inline 2024-08-14 20:26:55 +03:00
Bananymous 957df08932 Kernel: root command line option can be specified as an UUID
Format is the same as in linux
root=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

This makes it easier to boot when computer has multiple disks installed
2024-08-14 13:23:27 +03:00
Bananymous fd018b32d0 BAN: Implement GUID::to_string 2024-08-14 13:23:27 +03:00
Bananymous e000c7d818 BAN: Use static_cast instead of C-style cast in RefPtr deletion 2024-08-14 13:23:27 +03:00
Bananymous bb40069773 Kernel: Add nice debug prints for device addition/removal 2024-08-14 13:23:27 +03:00
Bananymous 37d5b60f5c Kernel: Implement AML {Event,Reset,Signal,Wait}Op 2024-08-14 11:59:34 +03:00
Bananymous 37e6cd6500 General: Update README contribution information 2024-08-14 10:34:41 +03:00
Bananymous feadea0e91 Kernel: Fix AML unaligned integer reads and buffer shifts over 32 2024-08-13 23:44:17 +03:00
Bananymous f71a29b6c4 Kernel: Implement AliasOp for AML interpreter 2024-08-13 22:42:37 +03:00
Bananymous ec4cfdee23 Kernel: Fix and cleanup a lot of AML code
Node now have APIs to convert them to buffer, integer and string. This
allows possibility to handle methods that need explicitly use one of the
overloads instead of integer.

This patch also adds handling of DebugOp. This is used quite heavily in
uACPIs test suite.
2024-08-13 22:42:37 +03:00
Bananymous dd79db6383 Kernel: AML implement CreateFieldOp 2024-08-13 18:52:48 +03:00
Bananymous 723e458bd7 Kernel/Terminal: Update terminal color themes and fix TTY bright/dark 2024-08-12 21:15:55 +03:00
Bananymous 25a47f0df3 LibC: Implement ungetc()
I didn't really test this, but I think this implementation does what it
needs to :)
2024-08-12 19:14:55 +03:00
Bananymous 39be57424c Kernel: Fix SYS_SEEK with SEEK_END
I was subtracting the offset from file end when I should have added it.
2024-08-12 19:13:27 +03:00
Bananymous 45ffa1b79c General: Update README feature list
README did not contain anything about GUI or USB which I have had for
maybe a month now :D
2024-08-12 15:14:04 +03:00
Bananymous f37e1c2229 Kernel: Map .rodata as read only instead of read-write 2024-08-12 14:28:52 +03:00
Bananymous 1bd7b86e60 Terminal: Implement cursor blinking
This looks much nicer than the old static cursor
2024-08-12 14:24:17 +03:00
Bananymous 18e7cf2069 Terminal: Add cursor rendering
Cursor is now shown at the current position. It can be hidden (or shown)
with the ansi `\033[?25h` or `\033[?25l`
2024-08-12 00:50:06 +03:00
Bananymous 60b4d90608 LibGUI: Add Window::get_pixel()
This is fine as its not reading from video memory
2024-08-12 00:49:35 +03:00
Bananymous 5197000124 LibC: Fix qsort for types bigger than 64 bytes 2024-08-11 18:00:05 +03:00
Bananymous 71d4060993 Ports: Update port building instructions 2024-08-11 17:58:10 +03:00
Bananymous 34c6ad7277 Base: Add sample png image and make it the default GUI background 2024-08-11 01:16:23 +03:00
Bananymous 9e79ef2a91 Terminal: Use pseudo terminal instead of pipes
Terminal can now send signals from keyboard (ctrl+c) to programs running
in the terminal!
2024-08-11 01:02:59 +03:00
Bananymous 4d1b32f770 Kernel: open does not need TTY_INIT to set the controlling terminal 2024-08-11 01:02:59 +03:00
Bananymous a5a097fa4a Kernel/LibC: Add initial pseudo terminal support
This patch implements posix_openpt() and ptsname()

grantpt() and unlockpt() are left in LibC as stubs, as posix_openpt
currently does all of the needed work.
2024-08-11 01:02:59 +03:00
Bananymous ad645f31d0 Kernel/LibC: Add setsid() 2024-08-11 00:58:12 +03:00
Bananymous f08d429851 Terminal: Add utf8 parsing for input
This patch adds possibility to render multibyte utf8 codepoints!
2024-08-11 00:54:51 +03:00
Bananymous bac3219a01 Kernel: Fix `Pipe::can_read_impl()`
The logic was inversed, which made all select calls report incorrectly
for pipes. This made terminal emulator just freeze.
2024-08-10 18:18:25 +03:00
Bananymous 09a527fb12 Kernel: Fix scheduler thread counter
I was actually never incrementing the counter :D
2024-08-10 18:18:25 +03:00
Bananymous 58a3a480b9 Ports: Update the port system once again! 2024-08-10 18:18:25 +03:00
Bananymous a12bfe4639 LibC: Don't crash when calling tcflush() 2024-08-09 17:03:21 +03:00
Bananymous 6cda639869 LibC: Add stubs for tmpfile, mktemp and fchmod 2024-08-09 17:02:49 +03:00
Bananymous 2797fe116f LibC: Add stubs for some functions in {dlfcn,ftw,utime,wchar}.h 2024-08-09 17:01:41 +03:00
Bananymous e768cd53fb LibC: Add definition for SIGWINCH
This is actually not yet sent, but programs can install signal handlers
for it!
2024-08-09 17:00:13 +03:00
Bananymous 83e2ad40d6 LibC: Implement gai_strerror and add stubs for {get,free}addrinfo 2024-08-09 16:59:49 +03:00
Bananymous 7ebd0699e3 LibC: Add _PC* definitions to unistd.h
These are not used for anything yet
2024-08-09 16:58:58 +03:00
Bananymous 46b1d4d194 LibC: Remove cxx abi stuff from libc
These will cause multiple definitions when linking with libstdc++
2024-08-09 16:58:11 +03:00
Bananymous f60e265397 LibC: Add ldexp to math.cpp 2024-08-09 16:57:16 +03:00
Bananymous 2e642327ea LibC: Implement localeconv() 2024-08-09 16:56:48 +03:00
Bananymous a87ce41030 LibC: rename fd_set internal variable
binutils seems to need access to the internal type and uses this name
for lookup.
2024-08-09 16:55:52 +03:00
Bananymous 0c8cae4cc3 LibC: Move ioctl to <sys/ioctl.h>
Some ports seem to be assuming that ioctl exists there and not in
stropts.h
2024-08-09 16:54:30 +03:00
Bananymous ed325b4a45 Kernel: Fix typo in userspace address validation 2024-08-09 16:52:57 +03:00
Bananymous 1c67b5e812 Kernel: Fix wait syscall to report status of exited children 2024-08-09 16:52:35 +03:00
Bananymous b6c964c444 Kernel: Rewrite pipes
Pipes have now a fixed size buffer and pipe clone and close is working
again.
2024-08-09 16:50:19 +03:00
Bananymous 6fedf06150 LibC: Implement {sig,_,}{longjmp,setjmp} 2024-08-09 15:58:56 +03:00
Bananymous 91d513a672 2000th COMMIT: userspace: Implement basic fetch program bananfetch
This patch adds a obligatory fetch program to banan-os!

This program (`bananfetch`) fetches some basic information about the
operating system and the hardware its running on!
2024-08-09 15:58:56 +03:00
Bananymous 44f0ec601f Kernel: Expose /proc/meminfo and fix /proc/<pid>/meminfo
Thread was unconditionally calling <stack>->size() without validating
that the stack actually exists
2024-08-09 15:58:56 +03:00
Bananymous 2a659a9d03 BuildSystem: Fix enable sse definition
__enable_sse was never actually defined for any targets. This also adds
__arch definition for libc (so `utsname` works).
2024-08-09 15:52:42 +03:00
Bananymous 7e7c3a1bb3 Kernel: VirtualTTY now handles dark colors
I have seemingly forgot to add these before
2024-08-09 15:52:42 +03:00
Bananymous 3b23458ecc LibC: Start work on locales
This patch adds 2 locales, POSIX locale and UTF8 locale.

functions `mbstowcs()` and `strcoll()` use locales to do convertions and
comparison respectively.
2024-08-09 15:52:42 +03:00
Bananymous 7afdfb150f LibC: Rewrite sigprocmask in terms of pthread_sigmask
Also don't fail SYS_SIGPROCMASK if how is invalid and set is NULL.
2024-08-07 17:01:35 +03:00
Bananymous 2ca7886f88 Ports: remove architecture from .compile_hash files
This is was over complicating things and there can be only one banan-os
build at a given time.
2024-08-07 16:34:28 +03:00
Bananymous 5aca6c7c1f Ports: Fix bugs in the port system
Fix some bugs and add pkg-config for the port system. Also ncurses is
now downloaded from the gnu mirror in the hope that it works better
2024-08-07 01:57:44 +03:00
Bananymous fe94d6cf89 LibC: Implement dummy rename() so vim can save files
vim calls rename when saving a file. This patch adds dummy
implementation that only works for regular files and does a copy instead
of proper rename.
2024-08-05 20:16:05 +03:00
Bananymous 0a7c316ffb Kernel: Fix race condition when blocking threads
If thread was blocked, but had not reached block queue, you might
already get an unblock request which would fail on an assertion.

If blocked thread was load balanced to another processor and unblocked
simultaneously, there was a race condition.
2024-08-05 20:14:13 +03:00
Bananymous e72424e01a Kernel: Implement ANSI CSI M and fix ANSI CSI L
There are not maybe not correct, but work much better than the old ones
2024-08-05 15:53:01 +03:00
Bananymous dbba9128a4 Ports: Add ncurses and vim port!
vim is kind of buggy because my virtual tty {insert,delete} line ansi
codes don't really work.
2024-08-05 15:20:18 +03:00
Bananymous 352c1ddc16 BuildSystem: Rewrite port compilation system
Now ports define some environment variables, and call a unified bash
script that handles configuration, recompilation and installation.

If a port uses non-configure buildsystem, port script can also specify
custom build and install step.
2024-08-05 15:20:18 +03:00
Bananymous 4d96ae56ac LibC: Add stubs for ungetc(), tcflush() and symlink() 2024-08-05 01:48:24 +03:00
Bananymous adadb10b15 LibC: Fix strsignal() and implement psignal() 2024-08-05 00:59:03 +03:00
Bananymous a749b9806e LibC: Add definition for h_errno
Nothing uses this yet, but linking will succeed
2024-08-05 00:58:06 +03:00
Bananymous 615d9d4abe LibC: Add INADDR_LOOPBACK
Some port was using this. Loopback addresses are not supported, but they
will be 127.0.0.1.
2024-08-05 00:56:42 +03:00
Bananymous aa03274093 LibC: Remove O_DIRECTORY from opendir()
opendir() did not work on symlinks after with this flag...
2024-08-05 00:55:03 +03:00
Bananymous e7a06979ec LibC: Implement usleep()
This is not a POSIX function, but some ports seem to be using it either
way
2024-08-05 00:54:17 +03:00
Bananymous 3651306f57 LibC: Implement strto{u,i}max() 2024-08-05 00:53:27 +03:00
Bananymous 5dce4e6aea LibC: Implement strftime()
I did not test this at all, this might very well be broken
2024-08-05 00:49:44 +03:00
Bananymous 1d9041f2aa LibC: make opendir() fail if path is not a directory 2024-08-01 22:57:46 +03:00
Bananymous a578527012 Kernel/LibC: Implement ioctl(TIOCGWINSZ)
This allows ncurses to get the window size!
2024-08-01 22:56:26 +03:00
Bananymous af78a2d080 Kernel: Implement ANSI CSI L for TTY
vim seems to be using this, so I decided its needed
2024-08-01 22:08:46 +03:00
Bananymous 840000d02b Kernel: Make sure null signal is not send with kill() 2024-08-01 21:41:54 +03:00
Bananymous 9ea4c777ad Kernel: TTY now handles ESC key presses
This allows exiting vim :D
2024-08-01 21:41:11 +03:00
Bananymous a33b63d066 Kernel/LibC: Implement alarm() and setitimer()
This makes vim able to start!
2024-08-01 21:09:56 +03:00
Bananymous da3b30cd94 Kernel: Add macro to dump all syscalls and their return values 2024-08-01 18:21:49 +03:00
Bananymous 4599e1dec5 Kernel: Make schedler not crash when unblocking active thread
This was happening when sending signals to processes that were currently
running.
2024-08-01 18:21:49 +03:00
Bananymous 6de350ce9d Kernel/LibC: Cleanup, fix and implement a lot of signal code
This patch implements sigsets and some of their usages
2024-08-01 17:01:18 +03:00
Bananymous 838d31fa41 Kernel: Implement more POSIX compliant open() and openat() syscalls 2024-08-01 15:35:02 +03:00
Bananymous 401b460d75 LibC: Implement setbuf() and setvbuf() 2024-08-01 01:30:00 +03:00
Bananymous c440204fa5 LibC: Implement dummy strcoll() 2024-07-31 23:58:10 +03:00
Bananymous adf50dffd8 LibC: Implement uname() 2024-07-31 23:54:26 +03:00
Bananymous 20d38ed28c LibC/Shell: Implement gethostname() and shell uses it for hostname 2024-07-31 23:53:55 +03:00
Bananymous edc30cd71d LibC: Compile without exceptions
This allows libc to not require __gxx_personality_v0. I can maybe add
C++ back to libc... :D I don't know why I did not research earlier what
this symbols was used for
2024-07-31 23:33:41 +03:00
Bananymous 10ce03a1e7 LibC: Implement ctime() and asctime() 2024-07-31 23:26:10 +03:00
Bananymous 5fca5c774a LibC: Implement umask() 2024-07-31 23:26:10 +03:00
Bananymous fc6c39e670 LibC: Implement gettimeofday() 2024-07-31 23:26:06 +03:00
Bananymous 7c3b6307d9 Kernel: Cleanup USB initialization code 2024-07-31 23:23:44 +03:00
Bananymous e52dac3b25 LibC: Implement tmpnam()
This implementation is not really spec compliant as rand() does not
guarantee TMP_MAX different outputs + seeding
2024-07-30 12:02:30 +03:00
Bananymous 62db9a8ef3 LibC: Implement C++ static guards 2024-07-30 12:02:05 +03:00
Bananymous f0be4f86a6 LibC/Kernel: Implement access 2024-07-30 11:51:07 +03:00
Bananymous e5bb843059 LibC/Kernel: Implement ttyname 2024-07-30 11:41:18 +03:00
Bananymous ca774dfeb5 LibC: Implement setlocale() for C locale 2024-07-30 11:33:02 +03:00
Bananymous 681d8327f5 LibC/Kernel: Cleanup termios code
This is still not correct, but much better than it used to be
2024-07-30 11:10:43 +03:00
Bananymous 9bc02c81f8 Kernel: Map DMA and PCI MMIO as uncached 2024-07-30 11:10:08 +03:00
Bananymous bb1738db8c Kernel: Make thread unblocking O(1)
This is still bit broken. VirtualBox seems to freeze sometimes, but I
could not recreate this on qemu (with and without kvm) or real hardware.
2024-07-24 00:31:01 +03:00
Bananymous 9548c592a3 Kernel: Always print basic info about xHCI controller
Its nice to see that the controller is getting initalized
2024-07-23 09:09:08 +03:00
Bananymous cda0276d39 Kernel: Force PCI irq line usage when not using APIC
afaik PIC does not support MSI
2024-07-23 09:08:00 +03:00
Bananymous 539afb329a Kernel: All processors use LAPIC timer when running with APIC
This makes scheduler preemption much cleaner as bsb does not have to
send smp messages to notify other processes about timer interrupt.

Also PIT percision is now "full" 0.8 us instead of 1 ms that I was using
before.
2024-07-23 02:28:52 +03:00
Bananymous 3e0150f847 Kernel: Pressing F1 now toggles rendering of CPU loads to the terminal
This can be nice for seeing the performance and problems on the load
balancing algorithm.
2024-07-22 00:43:13 +03:00
Bananymous f8261c60c0 Kernel: Rewrite the whole scheduler and re-architecture SMP handling
Change Semaphore -> ThreadBlocker
  This was not a semaphore, I just named it one because I didn't know
  what semaphore was. I have meant to change this sooner, but it was in
  no way urgent :D

Implement SMP events. Processors can now be sent SMP events through
IPIs. SMP events can be sent either to a single processor or broadcasted
to every processor.

PageTable::{map_page,map_range,unmap_page,unmap_range}() now send SMP
event to invalidate TLB caches for the changed pages.

Scheduler no longer uses a global run queue. Each processor has its own
scheduler that keeps track of the load on the processor. Once every
second schedulers do load balancing. Schedulers have no access to other
processors' schedulers, they just see approximate loads. If scheduler
decides that it has too much load, it will send a thread to another
processor through a SMP event.

Schedulers are currently run using the timer interrupt on BSB. This
should be not the case, and each processor should use its LAPIC timer
for interrupts. There is no reason to broadcast SMP event to all
processors when BSB gets timer interrupt.

Old scheduler only achieved 20% idle load on qemu. That was probably a
very inefficient implementation. This new scheduler seems to average
around 1% idle load. This is much closer to what I would expect. On my
own laptop idle load seems to be only around 0.5% on each processor.
2024-07-22 00:33:50 +03:00
Bananymous 9f90eeab05 Kernel: Fix stack pointer OOB check
i686 does not push the stack pointer on interrupt when no CPL change
happens.
2024-07-22 00:27:08 +03:00
Bananymous 1ee37cb671 Kernel: Make sure stack is aligned on interrupts for i686 target
I just realized that only x86_64 aligns stack pointer when interrupt is
triggered.
2024-07-22 00:10:42 +03:00
Bananymous 8fe798de6d Kernel: Make PIT counter atomic with spinlock 2024-07-21 18:56:41 +03:00
Bananymous e00efca170 Userspace: Pprogram template and loadfont mark install as optional 2024-07-21 17:41:40 +03:00
Bananymous 86b6714777 Kernel: Add command line option to boot without smp 2024-07-21 17:40:57 +03:00
Bananymous 77b5e6d44a Kernel: Make ATABus use BAN::Atomic<> instead of gcc builtin atomics 2024-07-21 17:39:26 +03:00
Bananymous ffe73165f9 Kernel: Fix PCI typo serail -> serial 2024-07-21 17:36:29 +03:00
Bananymous 96c7e9e29d Kernel: Cleanup VirtualRange code
Virtual range does not need to look into the page table for mapped
physcial pages. It can (and should) just keep track of them itself.
2024-07-21 17:35:07 +03:00
Bananymous 02051ed60f Kernel: Move keyboard kernel panic to the general input device
This will allow command to work without requiring it per keyboard driver
2024-07-21 17:33:38 +03:00
Bananymous f49689caac Kernel: Implement API for sending IPIs for only one processor
Also move the spin wait to check for pending IPI sends to the start of
ipi sending function. There is no need to wait for IPI send succeeding
right after it. It is enough to make sure there are not multiple IPIs
being sent at the same time.
2024-07-21 17:30:59 +03:00
Bananymous 819c130366 BAN: If format string is started with space, numbers are padded with it 2024-07-19 21:39:31 +03:00
Bananymous 974b9b992d Kernel: Fix process exiting infinite loop
If process was exiting and had a process waiting for it and that waiting
process got interrupted, the exiting process could never exit.
2024-07-18 21:40:44 +03:00
Bananymous 3ab1214012 LibC: Fix _toupper and _tolower definitions
They were using C++ global namespace, which of course does not exist for
C targets.
2024-07-18 01:10:51 +03:00
Bananymous 4b917390ac Kernel: Fix sse state saving
This was broken when I added SMP support. This patch makes sse kind of
dumb as it is saved and restored on every interrupt, but now it at least
works properly... I'll have to look into how sse can get optimized
nicely with SMP. Simple way would be pinning each thread to a specific
processor and doing pretty much what I had before, but sse thread saved
in processor rather than static global.
2024-07-16 23:15:11 +03:00
Bananymous 7a0fb9a57f Kernel: Fix TTY scroll clearing first and/or last characters from line 2024-07-16 22:49:49 +03:00
Bananymous 58fcd2b2fe Kernel: Fix multi-interface USB device initialization 2024-07-16 22:29:18 +03:00
Bananymous bdcf058e52 README: update discord link 2024-07-16 21:10:32 +03:00
Bananymous 61f0043cd8 resolver: Fix infinite debug printing loop, if client disconnects 2024-07-16 15:05:07 +03:00
Bananymous dd7bfba8d5 ports: Compile with multiple jobs and fix curl install query 2024-07-16 15:04:46 +03:00
Bananymous 5b8fdbc82d LibC: Don't expose math.h in stdlib.h 2024-07-16 12:47:38 +03:00
Bananymous 6a1c677cbc Ports: Update doom port gitignore 2024-07-16 11:54:57 +03:00
Bananymous e30952efee Kernel: Implement key repeating for USB keyboard
USB keyboards don't send repeating keys automatically, so it has to be
emulated in software.
2024-07-16 11:54:57 +03:00
Bananymous fd8dc03ae9 BAN: Default initialize optional storage
This removes maybe uninitialized warning when using default initialized
optionals.
2024-07-16 11:19:16 +03:00
Bananymous 1337758660 Kernel: Make USB HID interfaces configure all endpoints 2024-07-16 00:23:26 +03:00
Bananymous 339e8a7910 Kernel: Fix USB keyboard state updating with variable reports 2024-07-16 00:03:00 +03:00
Bananymous a60b460701 Kernel: Update USB HID code to support multiple top-level collections
This allows me to use my laptops own keyboard!
2024-07-16 00:01:53 +03:00
Bananymous a5cb4057f9 Kernel: Implement unified input files for device hot-plugging support
/dev/keyboard and /dev/mouse can be read for events from any attached
keyboard or mouse respectively. This makes device hot-plugging support
pretty much automatic for TTY, GUI, and whatever takes input.
2024-07-15 22:11:15 +03:00
Bananymous 9d7f97ccd5 Kernel: Implement DevFileSystem::remove_device
This function cleanly removes the devices from the whole filesystem.

USB devices are now removed from the filesystem as soon as they are
destroyed.
2024-07-15 22:10:42 +03:00
Bananymous 0578d41500 Kernel: Implement WriteCombining memory
This makes framebuffer much faster on real hardware
2024-07-15 22:09:09 +03:00
Bananymous 42c3fa24f0 Kernel: Add support for HID Report ID and parsing all collections
Only the first top-level collection is used for the device, but that
seems to generally be what keyboard and mouse use for input.
2024-07-15 15:51:07 +03:00
Bananymous 60b396fee5 Kernel: Add mouse move and scroll event merging back
This makes mouse work much faster when reading can't keep up with the
amount of events.
2024-07-15 15:45:20 +03:00
Bananymous 4cd9252ff6 Kernel: Fix USB code
Fix USB Keyboard scan code table for bottom row of keyboard

Support multiple interfaces on a single USB device

Add usb mouse to default qemu settings
2024-07-15 11:48:48 +03:00
Bananymous 75875d3a8f Kernel: Set interval and average trb length on configure endpoint
Real controllers seem to require this while spec 4.8.2.4 says that they
should be left as zero.
2024-07-15 11:46:28 +03:00
Bananymous 86e9d92ecb Kernel: Take ownership of xHCI controller from bios 2024-07-14 03:31:35 +03:00
Bananymous baa4e6475a Kernel: Implement basic USB Mouse
This has the same problem I described in previous commit for keyboard
2024-07-14 02:11:32 +03:00
Bananymous ac5c77ee2c Kernel: Implement USB Keyboard
This is kinda hacky, as I had disable the PS/2 initialization so that
usb keyboard gets /dev/keyboard0. I should add device hot plugging
support for TTY and GUI...
2024-07-14 02:09:18 +03:00
Bananymous 1efc6a1385 Kernel: Implement simple USB HID driver
This should be easily expandable to add HID devices
2024-07-14 02:04:48 +03:00
Bananymous 442ea8a692 BAN: Remove Optional emplace constructor
This was kind breaking some initializations. Emplacing is still possible
through Optional::emplace
2024-07-14 01:53:50 +03:00
Bananymous 749be67df3 Kernel: Fix cxxabi for function static variable guards 2024-07-14 01:53:50 +03:00
Bananymous a97a574718 Kernel: Rewrite the whole input system
PS/2 code is now kind of messed up, but it works. Keyboards and mice are
now an abstract class that is automatically exposed to userspace. This
will make adding USB input much nicer.
2024-07-14 01:53:50 +03:00
Bananymous 110a45bee6 BAN: Implement Variant::has_value() and Variant::operator bool() 2024-07-12 20:37:56 +03:00
Bananymous f120da3aca Kernel: USB device now sets the current configuration 2024-07-12 11:26:06 +03:00
Bananymous 240684bc1f Kernel: Refactor some xHCI code and add new definitions 2024-07-12 11:25:24 +03:00
Bananymous 62003d96f3 BAN: Implement Optional::value_or
This will return value specified in the argument if optional is not
storing any value.
2024-07-12 11:22:10 +03:00
Bananymous e905634343 Kernel: Fix bugs and cleanup USB and xHCI code and API 2024-07-11 14:10:55 +03:00
Bananymous 14dce1abac Kernel: Start work on USB stack
Current code can enumerate all xHCI devices and detect their type based
on the class code on device or interface descriptors.
2024-07-10 12:06:00 +03:00
Bananymous 8ddab05ed3 BuildSystem: Cleanup userspace directory layout
userspace programs are now in userspace/programs
userspace tests are now in userspace/tests

This makes listing userspace projects much cleaner. Libraries were
already separated to their own directory, so other programs should also.
2024-07-03 09:18:02 +03:00
Bananymous 5dc441c4af Kernel/userspace: Implement KD_LOADFONT and loadfont program 2024-07-03 09:02:49 +03:00
Bananymous 940fb0d1fd Base: Add /var/www with dummy index.html 2024-07-02 21:34:32 +03:00
Bananymous f18c33563d Kernel: Fix PCI MSI-X allocation 2024-06-30 20:36:43 +03:00
Bananymous 10e8a54b2e General: Add more missing features to README 2024-06-29 22:13:33 +03:00
Bananymous f792976d6d BuildSystem: Don't fill mount dir if mounting fails 2024-06-29 22:12:36 +03:00
Bananymous 08cbd009ac Kernel: PCI don't report multi function bit with header_type() 2024-06-29 22:11:55 +03:00
Bananymous 3d4219bfee Kernel: Don't panic if nvme initialization fails 2024-06-29 22:07:55 +03:00
Bananymous d58a569660 WindowServer: Limit msync to 60 Hz and only sync necessary pages
This speeds up GUI a lot. I can now run GUI on real hardware at almost
60 Hz.
2024-06-29 19:00:58 +03:00
Bananymous fd3cf5d2b1 BuildSystem: Allow running qemu without kvm
If you set QEMU_ACCEL to empty string, build system won't try to look
for kvm support.
2024-06-29 19:00:10 +03:00
Bananymous 1a844426c3 Kernel: Add fast path for framembuffer sync if bpp == 32 2024-06-28 23:34:11 +03:00
Bananymous 42237a3bc8 Kernel: Implement fast scrolling for TTY 2024-06-28 23:15:03 +03:00
Bananymous 010c2c934b BAN: Write RefPtr and WeakPtr to be thread safe 2024-06-28 22:00:29 +03:00
Bananymous 48a76426e7 BAN: Add more APIs for Atomic and make compare_exchage take a reference 2024-06-28 21:47:47 +03:00
Bananymous 0c645ba867 LibGUI: Window now uses double buffering
This allows data in shared memory object be always up to date. With this
change window server can update lazily, and not necessarily on all
invalidate calls
2024-06-27 00:39:59 +03:00
Bananymous f538dd5276 test-tcp: Fix printing of "connection reset" when tcp connection closed 2024-06-27 00:39:22 +03:00
Bananymous 31568fc5a1 Kernel: Rewrite Sockets to not be TmpInodes
TmpInodes just caused issues because TmpFS kept them alive. There was
really no reason for sockets to even be stored inside a TmpFS...
2024-06-27 00:35:19 +03:00
Bananymous 44c7fde2f7 BAN: Fix Function requires clause argument forwariding 2024-06-27 00:33:50 +03:00
Bananymous cb07142832 Kernel: ACPI allow more calling or \_S5
Spec says that \_Sx must have 4 fields, but virtual box seems to only
have the two defined once. This patch allows shutodown on virtual box
2024-06-25 23:25:10 +03:00
Bananymous 60a05412c9 Kernel: ACPI implement SizeOf 2024-06-25 23:24:51 +03:00
Bananymous 0179f5ea09 Kernel: ACPI add \_OS string 2024-06-25 23:24:19 +03:00
Bananymous f671ed7e3f Kernel: ACPI implement integer stores to registers as copies
Before storing const integer and then modifying the register it would
error.
2024-06-25 23:23:52 +03:00
Bananymous 2fccff5a35 Kernel: Implement ACPI IndexOp into Strings 2024-06-25 23:23:00 +03:00
Bananymous cd41d5f6dd ports: DOOM install now downloads and installs doom1.wad 2024-06-25 19:57:15 +03:00
Bananymous 66905fcc08 http-server: Make http-server actually usable
http-server now implements basic static content from a specified
directory.
2024-06-25 19:36:10 +03:00
Bananymous af4b138094 Kernel/LibC: Implement realpath
realpath is implemented as a syscall. This is not really required but it
was the easiest way to get it working as there is already path
canonicalization at kernel level.
2024-06-25 19:32:40 +03:00
Bananymous 3c57e05a65 BAN: Implement hash for StringView 2024-06-25 19:28:45 +03:00
Bananymous 25099b4c98 Kernel: Don't validate O_SEARCH for non-directories 2024-06-25 19:27:55 +03:00
Bananymous 1ac7629459 BAN: Implement StringView::rfind() 2024-06-25 11:04:03 +03:00
Bananymous 95681a7a05 LibImage: Cleanup bicubic calculations 2024-06-25 11:04:03 +03:00
Bananymous d7b8458a56 Kernel: Fix TCP sending
TCP send was effectively always waiting for connection to close and then
return a value of 0.
2024-06-25 11:04:03 +03:00
Bananymous 67dfe0bcf3 BAN: Allow String::formatted to fail 2024-06-25 11:04:03 +03:00
Bananymous b1869bced4 BAN: Implement StringView::starts_with() 2024-06-25 11:04:03 +03:00
Bananymous 61aa1ea11f sudo: Fix installation permissions
I had accudentally removed SETUID bit from sudo. This required to
elevate the permissions.
2024-06-24 15:00:08 +03:00
Bananymous 20aa7c79d1 BuildSystem: Fix crt file installation
crt files should be installed always before userpace programs are
linked.
2024-06-22 17:30:00 +03:00
Bananymous 22548a3f4a BuildSystem: Fix crt file installation
crt files for userspace are now a dependency of libc, which means that
everytime libc gets installed, crt files will also install.

This fixes the problem when building libc
2024-06-21 17:10:58 +03:00
Bananymous 9e1b5cbaab BuildSystem: Cleanup CMake code to allow libc only installation
There was no way to just install libc which is required for stdlibc++
2024-06-21 01:45:14 +03:00
Bananymous 1488ec5a03 Userspace: Implement the most basic http server
This server just responds with static http "hello world" to every
incoming request
2024-06-20 13:29:01 +03:00
Bananymous 4f0457a268 Kernel: Rewrite a lot of TCP code and implement TCP server sockets
TCP stack is now implemented much closer to spec
2024-06-20 13:26:50 +03:00
Bananymous 30fdc2198f BAN: Add exchage() to Atomic<T> 2024-06-20 13:24:42 +03:00
Bananymous bce16cdd6e Kernel: Fix how socket closing works
Sockets are now closed only when they are not referenced any more. This
allows child process to close socket and still keep it open for the
parent.
2024-06-19 10:39:44 +03:00
Bananymous d5daa46ab8 General: Add default .vscode directory
The .vscode contains 2 C++ profiles that use my custom toolchain and
correct library include paths. One configuration is for kernel and other
for userspace
2024-06-19 10:16:20 +03:00
Bananymous ad6d95ba52 BuildSystem: Rework the whole cmake build system
Now files are installed using the install() command instead of manually
copying files to their destinations. This allows automatic recompilation
of headers that did not work previously
2024-06-19 09:40:03 +03:00
Bananymous 318ce5dec8 All: Fix a lot of compiler warnings from header files
While reworking build system, header files started to report warnings.
2024-06-18 23:02:10 +03:00
Bananymous 526d4369ce Ports: Don't throw error if _installed_ does not exist
This was annoying and I got multiple people reporting it as a possible
bug.
2024-06-18 13:16:33 +03:00
Bananymous c69919738b BuildSystem: Move all userpace libraries under the userspace directory
As the number of libraries is increasing, root directory starts to
expand. This adds better organization for libraries
2024-06-18 13:14:35 +03:00
Bananymous 1b5a01a6c9 LibImage: Add support for PNG images
I have not tested images with less than 8 bits per color or images with
indexed color as gimp could not export those. There is currently no
support for interlaced images.
2024-06-18 02:37:46 +03:00
Bananymous f233715b70 BAN: Add d{print,want,error}ln_if macros for userspace 2024-06-18 02:35:45 +03:00
Bananymous a58ac18fa0 BAN: Add move constructors to ByteSpan 2024-06-18 01:52:02 +03:00
Bananymous f1e366d36f Kernel: Free keyboard mutex while waiting for data to read
This was making select hang if one thread was trying to read from
keyboard.
2024-06-17 23:04:37 +03:00
Bananymous 301dcd78cc LibImage: Compile with -O3
This makes image resizing with cubic interpolation around 10x faster
2024-06-17 23:03:52 +03:00
Bananymous 65c6d62a15 LibImage: Start work on PNG decoding
This patch adds PNG decoder that currently only inflates the PNG's zlib
deflate stream
2024-06-17 22:58:59 +03:00
Bananymous aaf7a249c6 image: Add nicer error message if image could not be parsed 2024-06-17 22:58:59 +03:00
Bananymous 0bc8d49684 LibImage: Add image format probing
Instead of determining the image type in Image.cpp call image probing
functions for each supported image type
2024-06-17 22:14:43 +03:00
Bananymous 17ecbca204 BAN: Add network_endian_to_host 2024-06-17 22:14:43 +03:00
Bananymous 78928b7eb4 Ports: Add curl port! 2024-06-17 21:40:13 +03:00
Bananymous 5236e1ef0d LibC: Add dummy functions for rename() and strftime()
I don't really want to implement them right now, but they are required
to exist for some ports.
2024-06-17 21:02:37 +03:00
Bananymous be7ed8e74a Kernel/LibC: Implement {get,set}sockopt()
These are pretty much dummy functions in the kernel side. Only case that
is handled is SOL_SOCKET with SO_ERROR. This is hard coded to return no
error. Network stack is currently synchronous, so all errors are already
reported through synchronous network functions.
2024-06-17 20:56:48 +03:00
Bananymous 78bcb85679 LibC: Implement gethostbyname()
This is not a POSIX function, but curl seems to depend on it. It is
even deprecated on linux...
2024-06-17 20:55:50 +03:00
Bananymous b98bb9eb27 LibC: Add HOST_NAME_MAX definition 2024-06-17 20:55:17 +03:00
Bananymous cad55a4da5 Kernel/LibC: Implement getsockname for ipv4 sockets 2024-06-17 20:54:45 +03:00
Bananymous 511fc870a1 BAN: Mark RefPtr and WeakPtr operator bool() as explicit 2024-06-17 20:19:36 +03:00
Bananymous dafd2fecf7 LibC: Implement gmtime() and localtime()
localtime() just returns gmtime() as there is no timezone support
2024-06-17 16:51:41 +03:00
Bananymous 9c5cca784e LibC: Implement bsearch() 2024-06-17 16:51:24 +03:00
Bananymous 1138165308 LibC: Define PF_* macros on sys/socket.h 2024-06-17 16:50:03 +03:00
Bananymous d7eb321d58 LibC: Make assert() macro a void expression 2024-06-17 16:49:26 +03:00
Bananymous 15f8c7014f BAN: Add line endings to d{warn,error}ln 2024-06-17 16:48:56 +03:00
Bananymous dd64e2060e WindowServer: Add support for background images
WindowServer now looks in _$HOME/.config/WindowServer.conf_ for a
configuration file that can specify a background image.

Also add default background image /usr/share/images/sample.ppm to the
base sysroot provided in the git repo.
2024-06-16 00:28:09 +03:00
Bananymous 14d4551476 LibImage: Add rgba getter for Image::Color
Actually the format is 0xAARRGGBB which is what my framebuffer uses.
2024-06-16 00:23:02 +03:00
Bananymous e6549b0fe8 LibImage: Implement (bi)cubic interpolation
This is kind of slow but yields much nicer results compared to
(bi)linear interpolation. I should probably add gamma correction...
2024-06-15 23:05:10 +03:00
Bananymous 157e05f57c image: Implement --scale argument to scale image to framebuffer
Also fix bug where red and blue channels were flipped
2024-06-15 17:24:01 +03:00
Bananymous 96efd1e8b9 LibImage: Implement image resize using nearest or bilinear filters 2024-06-15 17:23:24 +03:00
Bananymous 672ce40618 LibImage: Move userspace image parsing to its own library
The image utility now uses this tool instead of parsing images on its
own.
2024-06-14 11:05:54 +03:00
Bananymous 05e9d76c77 BAN: Implement will_{addition,multiplication}_overflow 2024-06-14 11:04:29 +03:00
Bananymous ea7fc7f6c4 Kernel: Implement read-only FAT12/16/32 driver with long name support
You can now mount FAT filesystems! This code might not work perfectly
but my quick testing seemed to work on all (FAT12/16/32) variants.
2024-06-14 01:04:12 +03:00
Bananymous 6b1d5d28be Kernel: VFS root now has to be block device instead of partition 2024-06-14 00:19:12 +03:00
Bananymous a9b0bfa740 LibC: Make ino_t always 64 bit 2024-06-14 00:18:21 +03:00
Bananymous cc6b80a55b BAN: Optimize Vector copy assignment to reduce allocations
If vector contains enough elements, it will now replace old elements
instead of clearing and reallocating
2024-06-14 00:17:28 +03:00
Bananymous 6707989cd5 BAN: Implement same_as and add requires for BAN::Function with lambda
BAN::Function(lambda) now requires that the object is callable and
returns the correct type. This allows overloads with different
callback formats.
2024-06-14 00:15:48 +03:00
Bananymous 766439db6d Kernel: Start work on adding support for new filesystems
Old code tried to create ext2 filesystem from all devices.
2024-06-11 10:50:26 +03:00
Bananymous d4903caafa Kernel: Combine consecutive mouse move and scroll events
This makes mouse work much smoother when running without kvm.
2024-06-11 00:07:31 +03:00
Bananymous caa0111c79 BAN: Implement back() for CircularQueue 2024-06-11 00:05:11 +03:00
Bananymous ffacff67cf LibFont: Move PSF code to separate file 2024-06-10 16:10:05 +03:00
Bananymous f43a7fdfb4 Ports/doom: Doom now runs inside a window 2024-06-10 16:10:05 +03:00
Bananymous 7bb1a3906d BuildSystem: Run kvm-ok to determine kvm access 2024-06-10 16:10:05 +03:00
Bananymous 530c259e71 Kernel: Close unix domain socket when it gets destoyed
Inode closing is something that needs a complete rework. Currently all
sockets are closed when close() is called, which leads to connection
closing if you fork()/exec() with socket being marked as CLOEXEC.

Inodes should probably only be closed once they are not referenced
anywhere.
2024-06-03 18:06:01 +03:00
Bananymous 843a6851c4 Userspace: Start work on a terminal emulator
Terminal is still missing some ANSI codes, cursor and pseudo terminal
support.

Shell's builtin start-gui now launches a Terminal instead of test
windows.
2024-06-03 18:04:33 +03:00
Bananymous 234051d6bc Shell: Optimize drawing characters at the end of a command 2024-06-03 18:03:19 +03:00
Bananymous 981c0eb8bc Shell: Only set terminal properties if STDIN is a TTY 2024-06-03 18:02:49 +03:00
Bananymous 1066855532 LibGUI: Mark Window's server fd as CLOEXEC and expose it through API 2024-06-03 18:01:34 +03:00
Bananymous f2d6518311 LibGUI: Add new drawing APIs to LibGUI::Window 2024-06-03 18:00:50 +03:00
Bananymous 765ccfa18c Kernel: Deliver SIGCHLD on process exit and ignore it properly 2024-06-03 17:58:24 +03:00
Bananymous 201aee3119 LibInput: Implement key_to_utf8_ansi
This function outputs utf8 encoding of a key event or ansi code for
everything it is applicable (arrows, ctrl+..., ...)
2024-06-03 17:52:43 +03:00
Bananymous 65f299038d BAN: Implement traits {true,false}_type with integral_constant 2024-06-03 17:51:44 +03:00
Bananymous bd1290706a Kernel: Implement SharedMemoryObject cloning 2024-06-03 03:41:00 +03:00
Bananymous 939cbf46e4 BAN: Implement BAN::UTF8::to_codepoint() for single byte types 2024-06-03 03:39:57 +03:00
Bananymous aec5a09caf Kernel/LibC: Implement SYS_ISATTY and isatty() 2024-06-03 03:36:25 +03:00
Bananymous 6346d1b6c7 Shell: Add builtin command for starting window server and test windows 2024-06-02 17:27:40 +03:00
Bananymous 05ee242b80 WindowServer: Add window title to title bar and send close events 2024-06-02 17:27:09 +03:00
Bananymous 64be3f05a3 LibGUI: Add 10 second timeout for connecting to WindowServer 2024-06-02 17:25:17 +03:00
Bananymous cfdce9be61 BAN: Mark RefPtr and WeakPtr helper destructors virtual
Also fix a bug in WeakPtr::lock() which would assert false if the
underlying weak link was not initialized
2024-06-02 16:50:26 +03:00
Bananymous 446220494e Kernel: Unix domain sockets close can now be detected
When a unix domain socket is closed and it has a connection to another
socket, it will make the other socket readable and recv will return 0.

This allows detection of socket closing
2024-06-02 16:48:55 +03:00
Bananymous f12ffa92a0 LibFont: Font::get_glyph() now returns nullptr if glyph does not exist
This allows getting glyphs with a single hash lookup
2024-05-31 13:05:07 +03:00
Bananymous b2a4797d16 BAN: Fix dwarnln and derrorln stop color 2024-05-31 13:04:36 +03:00
Bananymous 8bfacb0091 Kernel: Implement deletion of SMO objects 2024-05-31 13:04:23 +03:00
Bananymous 0501f3bd99 Kernel: Move font code to its own library LibFont 2024-05-31 10:47:05 +03:00
Bananymous ae3ae6fd0e WindowServer: Fix partial invalidation over cursor 2024-05-31 03:20:21 +03:00
Bananymous 011a5f57e1 WindowServer: Add title bars and clean up code 2024-05-31 03:02:58 +03:00
Bananymous 84b3289a2a Kernel: Move Scheduler::yield() lock check after interrupts disabled
I have no idea why this solves a bug where current processor has
scheduler lock at the beginning of yield.
2024-05-31 02:56:39 +03:00
Bananymous b760892de2 Kernel: Make pselect use nanosecods instead of milliseconds 2024-05-31 02:56:17 +03:00
Bananymous 6840a8983c Kernel: Make sure MSB is not set on SMO keys 2024-05-29 20:01:12 +03:00
Bananymous a1b3490764 Kernel: Improve random number generation for unsigned types 2024-05-29 20:00:47 +03:00
Bananymous 076f1efecb Kernel: Fix 32 bit fast page locking
I forgot to change this when changing the lock type. 32 bit boots again
fine :D
2024-05-29 19:44:39 +03:00
Bananymous b23511edb1 LibC: Don't use BAN inside pwd.cpp
This made pwd.cpp require libstdc++ which meant that getlogin() also
needed libstdc++. This made build process of libstdc++ require itself
which is of course not possible.
2024-05-29 19:12:15 +03:00
Bananymous 53e572f072 Kernel: Fix s_fast_page_lock type on 32 bit target 2024-05-29 18:04:23 +03:00
Bananymous c2b6ba0d5a Userspace: Start work on GUI and WindowServer
Current implementation can create custom windows and each window has
its own framebuffer. When window wants to write its framebuffer to the
screen it will send a message to the WindowServer using unix sockets.
2024-05-29 16:00:54 +03:00
Bananymous d4d530e6c8 Kernel: Implement basic shared memory objects
These can allocate memory that can be shared between processes using
a global key. There is currenly no safety checks meaning anyone can
map any shared memory object just by trying to map every possible key.
2024-05-29 15:58:46 +03:00
Bananymous 99270e96a9 Kernel: Lock debug lock while printing fault details
This allows multiprocessor to dump clean output on concurrent faults
2024-05-29 15:49:24 +03:00
Bananymous 4bf7a08c80 Kernel: Allow SYS_PSELECT to work with timeout of zero 2024-05-29 15:32:18 +03:00
Bananymous 3823de6552 Kernel: Add templated get function for Random 2024-05-29 15:32:00 +03:00
Bananymous 30592b27ce BAN: Add comparison operators for RefPtr 2024-05-29 13:50:03 +03:00
Bananymous 8bc6c2eb20 Kernel: Move KeyEvent/MouseEvent from kernel to LibInput 2024-05-28 23:30:08 +03:00
Bananymous 87d52e5ebe Kernel: Fix timer early wake message
When printing early return message, current time was read twice. This
could lead to early return check failing, but when printing and reading
the time again subtraction overflow would happen.
2024-05-28 16:04:18 +03:00
Bananymous 598a09c13d Kernel: Allow select to work on any type of inode 2024-05-28 16:03:54 +03:00
Bananymous 54db4ab215 BAN: Increase BAN::Function storage size to 8 pointers 2024-05-28 16:01:41 +03:00
Bananymous 5b5ccba247 LibC: Define PATH_MAX in limits.h 2024-05-28 01:08:25 +03:00
Bananymous 18e2559b1e Kernel/LibC: Add SYS_TRUNCATE 2024-05-28 01:08:04 +03:00
Bananymous f5987b68ff BAN: Mark some class methods as constexpr 2024-05-28 01:07:29 +03:00
Bananymous a1ab44d39f Kernel: Optimize disk reads to read multiple sectors at once
Old StorageDevice::read_sectors() read each sector separately if the
underlying disk had a disk cache. This patch allows multiple sectors to
be read even if the disk cache exists and contains some of the sectors.

Only sectors that could not be found from the disk cache are actually
read from the disk. This optimization is not done for writing, which
still will write each sector separately, if disk cache has no memory to
store new sectors. It would feel kind of unnecessary optimization as you
have greater problems if disk cache cannot allocate a single page.
2024-05-27 15:52:34 +03:00
Bananymous 8b1514e575 Kernel: Make all storage devices readable and writable
I only had a {read,write}_impl defined for ATABaseDevice. This patch
moves that implmentation to general storage device.
2024-05-27 13:41:55 +03:00
Bananymous 2d3810874d Kernel: Fix thread signal handling
Threads will now only handle signals once they are not holding any
mutexes. This removes some dead locks.
2024-05-26 20:08:35 +03:00
Bananymous a4c634e4bf Sysroot: Fix us keymap bracket keys 2024-05-26 16:08:29 +03:00
Bananymous 2a4d986da5 Kernel: Add preliminary support for PCIe
Only segment 0 is supported, but devices can now be accessed through
mmio.

Adding more segments would require adding argument to every PCI API so
it is left for later.
2024-05-25 20:50:07 +03:00
Bananymous 3b18730af6 BuildSystem: Don't build libstdc++ as libc is not complete enough 2024-05-25 19:17:13 +03:00
Bananymous df260fe0e8 Kernel: Process::validate_pointer_access now maps the whole range
This fixes a bug where userspace provided address is not fully mapped
and the kernel tries to read/write it while using PageTable fast page.

In the future userspace input should be copied on syscall entry, so
userspace could not modify the input during syscall. Currently there
is change that userspace input passes kernel syscall validation and
after that userspace could modify the input before the value is
actually used.
2024-05-24 14:14:17 +03:00
Bananymous 2be4fe8404 Kernel: Make PageTable::s_fast_page_lock non-recursive
This lock is only used in wrapper of PageTable. There is no possiblity
of taking the lock outside of these wrappers.
2024-05-24 14:12:35 +03:00
Bananymous 7db7cfe20f BuildSystem: Only use kvm if user has rw access 2024-05-24 11:09:04 +03:00
Bananymous cc2cc2849e Whoami: Rewrite whoami using getlogin 2024-05-23 15:44:21 +03:00
Bananymous 06f4b0b29a BAN: Make String and StringView header only
This allows linking with libc without having to link ban
2024-05-23 15:43:26 +03:00
Bananymous e22821799b LibC: Implement getlogin() 2024-05-23 15:07:21 +03:00
Bananymous 14dd9294aa LibC: Add _POSIX constants to limits.h 2024-05-23 15:04:59 +03:00
Bananymous 83e3409bd8 Kernel/LibC: Update SYS_SEEK to return new offset and implement lseek 2024-05-23 14:49:23 +03:00
Bananymous 7630170ed6 LibC: Implement qsort as quick sort 2024-05-23 14:44:48 +03:00
Bananymous 0af74fccda Kernel/LibC: Rework dirent structure
dirent now contains statically sized d_name. This allows using
sizeof on the name and dirent properly, which some programs seem
to be using.
2024-05-22 20:19:59 +03:00
Bananymous e77de1804f Kernel: Fix some race conditions in TCP stack
Remove race condition if two acks are to be sent one after another.

Always unblock semaphore once TCP thread has done something. This
allows better chance of TCP sending to succeed.

There are multiple places in the networking code that would require
thread-safe entering to blocking mode. I should add some API for this
so that a lot of race conditions could be removed.
2024-05-21 01:53:45 +03:00
Bananymous e00b92225d Kernel: Fix E1000 interrupt handling condition
I had written the ICR register check backwards which lead to interrupt
handling only when it was not needed, and no handling when it was
needed. This somehow still worked, just much slower often requiring tcp
resends from the server.
2024-05-21 01:52:19 +03:00
Bananymous ee8fca9439 General: Update discord badge 2024-05-20 21:07:15 +03:00
Bananymous adc562feb8 General: Add simple contribution guide lines
README now contains simple contribution guide lines that should be
followed. I am now more open for public contributions to the repo
if people are interested.

Create a discord server and link it to the README. Discord seems to be
a nice platform for general messaging.
2024-05-20 20:44:34 +03:00
Bananymous af8fa4014f BuildSystem: Always download cmake instead of using host's
If cmake was installed from snap, build system would cause an error
because some libfakeroot GLIBC requirement.
2024-05-20 20:11:13 +03:00
Bananymous 08415b0e8f General: Remove submodule info from README
Submodules are no longer needed as I now use my own AML interpreter instead of lai
2024-05-12 02:14:46 +03:00
Bananymous 967f9a9174 Shell: Ignore tab as that just fucks up formatting 2024-05-07 14:16:17 +03:00
Bananymous d255141bd4 BuildSystem: Cleanup image creation 2024-04-24 01:27:59 +03:00
Bananymous e7e1dd91c7 Kernel: Implement ACPI reset 2024-04-22 21:12:13 +03:00
Bananymous 195c5e92a4 Kernel: Add floating bus detection for ATA Bus 2024-04-22 21:12:13 +03:00
Bananymous 47dafe62b8 BuildSystem: Make port build script use bash as shell
pushd and popd don't work in sh
2024-04-22 21:12:13 +03:00
Bananymous 26922ebb51 Kernel: Remove stack size check for keyboard layout initialization
Keyboard layout loading can take around 1 KB of stack for i686 target
2024-04-22 21:11:04 +03:00
Bananymous 7480e71801 BuildSystem: Download binutils and gcc as tar balls
I am not sure why I changed this earlier. This is much cleaner and
faster as git doesn't have to recieve all patches and resolve deltas
2024-04-22 19:54:57 +03:00
Bananymous 9ac3f48fcb Bootloader: Implement better memset and memcpy for 32 bit addresses 2024-04-20 18:14:05 +03:00
Bananymous 0e405755ad General: Update README badge
Tokei's own badge was kind unreliable and slow. I now parse the tokei output into json on server side and make shields.io query it.
2024-04-20 10:39:50 +03:00
Bananymous 693f90449f Kernel: Rework AML package and implement indexing in to packages 2024-04-19 11:26:48 +03:00
Bananymous 0bf45069bd General: Add link to online demo in README.md 2024-04-18 15:35:31 +03:00
Bananymous 34b10f61ce Kernel: Make PIT reserve its IRQ
PIT did not reserve IRQ leading to kernel panic if it was being
initialized.
2024-04-18 13:34:28 +03:00
Bananymous 1479b42112 Kernel: Don't even loop over processor when system has only BSP 2024-04-18 13:33:52 +03:00
Bananymous bb061d2a0a Kernel: Make 32bit not use large pages during boot
There is no guarantee that they are supported
2024-04-18 13:33:15 +03:00
Bananymous 061012a268 Kernel: Fix signal trampoline
32 bit did not even support it and 64 bit did not align stack
2024-04-18 13:32:40 +03:00
Bananymous a698f91db4 Kernel: Fix PriorityMutex::try_lock()
Forgot to check this when I updated Mutex::try_lock()
2024-04-18 01:36:15 +03:00
Bananymous 30d12a76bc Kernel: Fix 32 bit compilation 2024-04-18 01:35:56 +03:00
Bananymous 687fa44eff Kernel: Add support for ACPI general purpose events 2024-04-17 21:51:03 +03:00
Bananymous aefb33efff Kernel: Implement dummy AML NotifyOp 2024-04-17 15:40:11 +03:00
Bananymous da0c45b7ee Kernel: Cleanup AML debug dump output 2024-04-17 15:35:42 +03:00
Bananymous 956335e844 Kernel: Implement AML WhileOp 2024-04-17 15:00:14 +03:00
Bananymous 701fc600cd Kernel: Implement evaluate and store for BankFieldElement 2024-04-17 14:59:51 +03:00
Bananymous e38b2cff4f Kernel: Implement proper synchronization for AML opregions 2024-04-17 14:59:26 +03:00
Bananymous b268293402 Kernel: Fix Buffer evaluate and store for sizes >= byte
All reads and writes were to the first byte. This patch adds the
correct offset for reading and writing.
2024-04-17 14:57:13 +03:00
Bananymous 45b9dc8be9 Kernel: Make ACPI \\_OSI return true for Linux 2024-04-17 02:19:35 +03:00
Bananymous 0ad7025a17 Kernel: Actually map FACS (and global lock) 2024-04-17 02:14:56 +03:00
Bananymous 49b7467840 Kernel: Simplify AML method invocation API 2024-04-17 01:20:58 +03:00
Bananymous a40ef610a2 Kernel: Allow demand paging only for userspace threads 2024-04-17 01:16:43 +03:00
Bananymous f9943b60e4 Kernel: Fix mutex try_lock return value
WHY IS GCC NOT COMPLAINING FROM HEADERS... I used almost 2 hours
trying to locate why AML was broken
2024-04-17 01:16:43 +03:00
Bananymous f97fb1b35d Kernel: Evaluate _REG on devices for embedded controller if needed 2024-04-17 01:16:43 +03:00
Bananymous b959181afd Kernel: Remove ACPI namespace from itself
This removes unnecessary cyclic reference
2024-04-17 00:52:05 +03:00
Bananymous cbc27a94ac Kernel: AML cleanup IfElse code 2024-04-17 00:49:07 +03:00
Bananymous 6a4f999b88 Kernel: Cleanup AML code and add evaluate for string 2024-04-16 17:39:16 +03:00
Bananymous 7707e01352 Kernel: AML add flag to force absolute lookup for ACPI namespace 2024-04-16 17:38:23 +03:00
Bananymous e667326df5 Kernel: Fix AML if parsing
Parse else case unconditionally, even if _if_ case evaluates to true
2024-04-16 17:36:04 +03:00
Bananymous f1b2d7530d Kernel: Rework AML namespace and object hierarchy
Remove tree-like structure from AML. This allows more spec compliant
parsing of named objects inside not yet declared devices.

This also allows AML to be run thread safely. All object adds/removes
are now guarded by a mutex.
2024-04-16 16:47:45 +03:00
Bananymous b6587b32b9 Kernel: ACPI ignore events from non-existent ports 2024-04-16 00:00:07 +03:00
Bananymous b89bafa165 Kernel: Add support for power button shutdown
This patch implements basic support for power button using ACPI
fixed events. I still need to implement general purpose events
and embedded controller for full power button support.
2024-04-15 23:55:25 +03:00
Bananymous 9fac5f94ba Kernel: Fix entering ACPI mode
My condition to check wheter to enter ACPI mode was incorrect.
I only entered ACPI mode if I already was in ACPI mode :DD
2024-04-15 15:05:48 +03:00
Bananymous 5affc73ee6 Kernel: Parse ACPI namespace only after TTY is initialized
This allows better debugging when there is no serial port available.
2024-04-12 16:47:17 +03:00
Bananymous 027016ddae Kernel: Load all SSDTs and PSDTs
Spec says to load all tables with unique OEM table IDs but that seem
wrong.
2024-04-12 16:45:00 +03:00
Bananymous 8f2f98b7b4 Kernel: AML implement SleepOp 2024-04-12 16:17:14 +03:00
Bananymous 6b43d12469 Kernel: AML package doesn't mark ZeroOp as NullName reference 2024-04-12 16:10:16 +03:00
Bananymous 74940ed33c Kernel: Cleanup AML code and fix bugs
I can enter ACPI mode on my own laptop!
2024-04-12 16:03:14 +03:00
Bananymous 17871bb3ca Kernel: Fix ACPI namespace initialization
ACPI spec says that only SSDTS with unique OEM table IDs are to be
loaded.

Add loading of ACPI 1.0 PSDTs
2024-04-12 12:41:30 +03:00
Bananymous 89c4abc07a Kernel: Cleanup AML device initialization
_STA and _INI are now properly called on call devices
2024-04-12 02:00:30 +03:00
Bananymous 46b5a7697c Kernel: Implement dummy \_OSI method for AML
This always returns Zero (not supported)
2024-04-12 01:49:54 +03:00
Bananymous dd8060d64f Kernel: Add FieldElement access attribute for AML interpreter 2024-04-12 01:49:15 +03:00
Bananymous afb1d7ef0c Kernel: Implement more features for AML parser/interpreter
Added
   - BankField
   - BufferField
   - PowerResource
   - ThermalZone
   - Reference
   - Package element forward declare
2024-04-12 01:47:40 +03:00
Bananymous 93ddee5956 Kernel: Implement locking for AML
Now global lock uses the actual global lock. Currenly if no lock
can be acquired, we just panic the kernel so that I remember to
implement it properly once AML is running concurrently.
2024-04-11 01:48:46 +03:00
Bananymous 0184e5beb5 Kernel: AML tries to initialize processors when entering ACPI mode
I had forgotten that Processors used to be a different definition
in AML.

I also implemented reads/writes for FieldElement/IndexFieldElement
that fit in 64 bits. Reads and writes to buffer are still a TODO.
2024-04-11 01:48:46 +03:00
Bananymous 3f2e110eab Kernel: Entering ACPI mode now actually enables ACPI
I used to only initialize devices, but now I send ACPI_ENABLE if
machine is not hardware reduced.
2024-04-11 00:17:03 +03:00
Bananymous 0ff68b7d66 Kernel: Make ACPI load all SSDT headers after DSDT is loaded 2024-04-10 15:03:54 +03:00
Bananymous cdbdc1a822 Kernel: Remove lai as a dependecy
I don't think lai is needed anymore, since my own AML interpreter
can do ACPI poweroff which was all that lai was used for.
2024-04-10 04:39:48 +03:00
Bananymous 7a2be05c69 Kernel: Implement poweroff with my AML interpreter
This can succesfully poweroff qemu!
2024-04-10 04:32:35 +03:00
Bananymous 5be38d0702 Kernel: My AML parser can now enable ACPI mode on QEMU! 2024-04-10 03:05:27 +03:00
Bananymous ff203d8d34 Kernel: Implement more AML method invocation stuff
Method invocation is starting to come together. This implemenetation
can interpret some of the qemu's functions to enter ACPI mode.

PCI config space access is currently the one thing is between
entering ACPI mode.
2024-04-10 01:52:14 +03:00
Bananymous 23fa39121c Kernel: Start working on AML method evaluations
Also fix namespace lookup and scope creations.
2024-04-09 18:37:51 +03:00
Bananymous b16e65168f Kernel: Rewrite whole AML parser
Now AML parsing is actually done while respecting namespaces and
scopes. I implemented the minimal functionality to parse qemu's AML.

Next step is to implement AML interpreting and then we can drop lai
as a dependency.
2024-04-09 01:16:07 +03:00
Bananymous 090a294017 BAN: Add {little,big}_endian_to_host
These just call host_to_{little,big}_endian but are more verbose
and cleaner.
2024-04-09 01:13:28 +03:00
Bananymous 22bc4b4271 Kernel: Fix AML package parsing 2024-04-07 20:32:22 +03:00
Bananymous e01c049401 Kernel: Fix AML buffer parsing 2024-04-07 20:24:05 +03:00
Bananymous e7ef7a9e55 Kernel: Implement barebones AML parser
This implements only parsing for AML in qemu. InvokeMethods are not
parsed since number of arguments to Methods is not yet known.

Parsing AML uses multiple kilobytes of stack space, so I increased
boot stack size by a lot :D

I am not sure where my own AML is going, but this is good start if
I decide to implement full ACPI on my own.

This code is very much just ugly macro expansion.

Qemu has 2 DefPackage elements that I am not able to parse. Package
data ends while there should be still multiple elements.
2024-04-07 17:03:30 +03:00
Bananymous e0011d22f2 Kernel: Move ACPI to its own directory and namespace 2024-04-04 15:00:13 +03:00
Bananymous 7d34bd8f82 Ports: Doom don't call exit() on I_Quit()
This seems to close the app prematurely :D
2024-04-04 00:40:51 +03:00
Bananymous bd69cf599b Ports: Add doom port
This patch contains simple infrastructure for porting software for
banan-os. I added a doom as the first port. Doom needs a wad file
that you have to aquire yourself. I am not sure if I am allowed to
redistribute doom1.wad (shareware) version so I decided not to.
2024-04-03 19:20:15 +03:00
Bananymous 1ac7de9ee5 General: Update README feature list
Features listed in README were kind of old. Banan-os now supports much more. Update lists to match current progress.
2024-04-03 16:31:09 +03:00
Bananymous 397043a9c0 General: Rename LICENCE -> LICENSE 2024-04-03 15:59:30 +03:00
Bananymous 43ff03e33b Update README badges
Add more badges now that I have github mirror and badges from shields.io and tokei.rs work!
2024-04-03 15:57:09 +03:00
Bananymous fa900df5a7 Kernel: Add signals for threads after IRQs
This allows signals to be called even if the process does no syscalls
The old scheduler did signal handling but I feel like it should be
enough to handle them only after syscalls and IRQs. ISRs already
handle signals that caused the ISR and there is no other route to
kernel space.
2024-04-03 15:07:18 +03:00
Bananymous 414f0f6cd9 Userspace: Don't link with libc
This fixes bug where sometimes cmake does not find libc from sysroot

LibC is linked per program in its own CMakeLists.txt
2024-04-03 14:46:18 +03:00
Bananymous 7ef751ba95 Kernel: Fix multiprocessor for i686
i686 is now actually ran with multiple processors.
2024-04-03 14:42:17 +03:00
Bananymous f8c01418b1 Kernel: Fix multiprocessing on x86_64
I did not even start APs after initializing them... :D
2024-04-03 14:21:55 +03:00
Bananymous 731330c6b5 Merge pull request 'Add back x86_32 support' (#5) from x86_32 into main
Reviewed-on: Bananymous/banan-os#5
2024-04-03 02:36:28 +03:00
Bananymous d2df55b1ac Kernel: Allow booting with multiple processors on i686
Also remove unnecessary ds clearing in x86_64.
2024-04-03 02:30:38 +03:00
Bananymous 0dd74e3c9d Kernel: Implement syscalls for i686 and cleanup x86_64
This actually allows i686 to boot properly!
2024-04-03 02:23:23 +03:00
Bananymous 9e073e9fa0 Kernel: Add offset for interrupt stack in Scheduler::yield()
This allows accessing (garbage) sp and ss in interrupt stack.
2024-04-03 00:45:22 +03:00
Bananymous c95a271821 Kernel: Set ss in i686 tss 2024-04-03 00:43:38 +03:00
Bananymous fe386fa819 Kernel: Implement thread start trampoline for userspace
This is needed on i686 to set segment registers.
2024-04-03 00:42:39 +03:00
Bananymous 4d70322eab Kernel: Save segment registers on all interrupts on i686 2024-04-03 00:41:13 +03:00
Bananymous d9b8391968 Kernel: Fix i686 page table global mappings 2024-04-03 00:40:16 +03:00
Bananymous 6ac3681604 Bootloader: Implement loading for 32 bit ELF files. 2024-04-02 15:30:35 +03:00
Bananymous b35cad0c2e Bootloader allow installation when BANAN_ARCH=i686 2024-04-02 12:48:35 +03:00
Bananymous 2106a9e373 Kernel: Rework scheduler/processor stacks. 2024-04-02 12:34:42 +03:00
Bananymous 5050047cef Kernel: Rewrite whole scheduler
Current context saving was very hacky and dependant on compiler
behaviour that was not consistent. Now we always use iret for
context saving. This makes everything more clean.
2024-03-29 18:02:12 +02:00
Bananymous 1b65f850ee Kernel: Rename thread stacks to more appropriate names 2024-03-27 15:06:24 +02:00
Bananymous 7c2933aae1 Kernel: Fix ISR error code formatting to 32 bit 2024-03-26 21:01:18 +02:00
Bananymous 96babec22a Kernel: Implement Thread trampolines for x86_32 2024-03-26 21:01:18 +02:00
Bananymous c12d1e9bd9 Kernel: Implement PageTable for x86_32
This is mostly copied from x86_64 with necessary modifications
2024-03-26 20:16:20 +02:00
Bananymous 4d1f0e77f2 Kernel: Fix physical address size for x86_32
Having 32 bit address space does not mean physical address space
is also only 32 bits...
2024-03-26 20:16:20 +02:00
Bananymous d7bf34ecd0 Kernel: Write isr handler for x86_32 and cleanup x86_64 2024-03-26 20:16:20 +02:00
Bananymous 1943c3e7a1 Kernel: Unify IDT and GDT code between x86_64 and x86_32
The code is pretty much the same, so there are just couple macros
differiating initialization.
2024-03-26 16:42:02 +02:00
Bananymous af050cc729 Kernel: Fix boot code for x86_32
Boot assembly now initializes processor and jumps to kernel
2024-03-26 13:25:22 +02:00
Bananymous 84ef2161a1 BuildSystem: Allow running qemu with i686 target 2024-03-26 03:18:54 +02:00
Bananymous ca23360d07 Bootloader: Fix GDRT pointer size to 32 bits 2024-03-26 03:04:57 +02:00
Bananymous 5dbe51a52e Userspace: Update printf formats to compile on 32 bit 2024-03-26 03:03:33 +02:00
Bananymous 99e30a4d7d Kernel: Replace i386 with i686
I don't really want to be working with i386 since it doesn't support
compare exchange instruction
2024-03-26 02:48:26 +02:00
Bananymous 93975fdc45 Kernel: Process signal mask is now 2 32 bit values
This allows signal mask to be atomic on 32 bit target
2024-03-26 02:46:51 +02:00
Bananymous fbef90f7cb Kernel/LibC: Write cxx abi with proper locking 2024-03-26 02:28:10 +02:00
Bananymous a9db4dd9a3 Kernel: NVMe Queue max simultaneous commands is dependent on arch
This allows mask to be atomic on 32 bit architectures
2024-03-26 01:45:43 +02:00
Bananymous fc7e96fa66 Kernel: Rewrite i386 boot code + linker script 2024-03-26 00:10:42 +02:00
Bananymous 097d9a6479 Kernel: Implement dummy IDT and GDT for i386 2024-03-26 00:10:42 +02:00
Bananymous 2dd0bfdece Kernel: Make i386 thread tramplines crash 2024-03-26 00:10:42 +02:00
Bananymous 26585bb1d9 Kernel: Implement signal trampoline for i386 2024-03-22 15:41:15 +02:00
Bananymous 0d92719433 Kernel: Remove old i386 spinlock code 2024-03-22 15:41:15 +02:00
Bananymous 1ab2722850 Kernel: Add PageTable stub to progress linking 2024-03-22 15:41:15 +02:00
Bananymous fe17958b9f Kernel: Rename rsp->sp and rip->ip
This makes more sense if we support i386
2024-03-22 15:41:15 +02:00
Bananymous 3e4d410646 Kernel: Fix AHCI device physical address writing on i386 target 2024-03-22 15:41:15 +02:00
Bananymous b5aae34d86 Kernel: Specify template paramenters where they cannot be deduced 2024-03-22 15:41:15 +02:00
Bananymous 7f029b2713 Kernel: Allow Processor compilation for i386 targets
This is achieved by rewriting some inline assembly and changing
ProcessorID to be 32 bit value. For some reason if processor id
is 8 bits gcc runs out of 8 bit registers on i386.
2024-03-22 15:41:15 +02:00
Bananymous 0424082e7b Kernel: Only compile lai for x86_64 targets
I will be dropping lai entirely soon. Once I get to writing AML
interpreter.
2024-03-22 15:41:15 +02:00
Bananymous 2352c86048 Kernel: i386 has 14 indirect blocks in TmpInode instead of 2
This allows keeping size of TmpInodeInfo as 128
2024-03-22 14:01:27 +02:00
Bananymous c0dff5e203 Kernel: Scheduler/Thread add inline assembly for i386 2024-03-22 14:01:27 +02:00
Bananymous d920785256 Kernel: RDRAND on i386 is called twice with 32 bit register 2024-03-22 14:01:27 +02:00
Bananymous 45cea14165 Kernel: Move sys_fork trampolines to kernel/arch/ directory 2024-03-22 12:48:54 +02:00
Bananymous 26ed689d30 Kernel: Remove old GDT, IDT and MMU code from i386
It will be easier to just rewrite them
2024-03-22 12:47:34 +02:00
Bananymous 7ce0370b6a Kernel: Define KERNEL_OFFSET for i386 target 2024-03-22 12:35:49 +02:00
Bananymous aa2e53c4f8 Kernel: E1000 fix physical address on 32 bit target 2024-03-22 12:35:38 +02:00
Bananymous 9ecd156622 Kenrel: Ext2 fix signed-unsigned comparisons 2024-03-22 12:35:29 +02:00
Bananymous 3c62be3f5d BAN: Implement make_signed and make_unsigned 2024-03-22 12:35:20 +02:00
Bananymous d1c8273826 Toolchain: Allow toolchain compilation for i386 targets
Fix GCC flags that were x86_64 specific
2024-03-21 21:59:09 +02:00
Bananymous 7d1b7436d4 LibC: Dummy ctr* files for i386
This allows compilation of libc for i386 targets
2024-03-21 15:20:20 +02:00
Bananymous 65750586b6 LibC: Use GCC builtins for math functions 2024-03-21 15:19:44 +02:00
Bananymous 62f6128ba1 Kernel: Cleanup NVMe Queue command submission
There is techically a race condition on thread sleep and checking
done mask. This patch allows read to success even if this race
condition is hit, although the full timeout has to be waited.

This can be fixed in future with some sort of wait queues that
can properly handle this race condition.
2024-03-19 13:01:27 +02:00
Bananymous 7f5c850744 Userspace: Add us keymap and make it default 2024-03-19 12:30:57 +02:00
Bananymous 9607b4205a Kernel: Fix kernel panic on signal
Signals are now added/handled without Scheduler's lock
2024-03-18 16:05:47 +02:00
Bananymous e447d5fccf All: remove obsolete gitignore files
These have not been in use for almost a year
2024-03-18 15:28:46 +02:00
Bananymous 090c3c9930 Kernel: NVMe queues now supports upto 64 simultaneous operations 2024-03-15 13:46:35 +02:00
Bananymous 48ea9e1c1d Kernel: PS2Controller uses RecursiveSpinLock so timeouts don't panic 2024-03-15 13:45:44 +02:00
Bananymous 42469b83fe Kernel: kernel panic is now sent to all processors 2024-03-15 13:45:01 +02:00
Bananymous e65bc040af Kernel: Now all active processors are used in scheduling
When a timer reschedule happens, ipi is broadcasted too all
processors for them to perform a reschedule!
2024-03-09 23:53:50 +02:00
Bananymous 89ca4c8a8b Kernel: Implement IPI broadcasting 2024-03-09 23:53:38 +02:00
Bananymous 2323a55517 Kernel: Debug lock is locked while dumping stack trace 2024-03-09 23:52:06 +02:00
Bananymous 45d6caa1d0 Kernel: APs now start their idle threads when scheduler is started 2024-03-09 23:51:40 +02:00
Bananymous 55d2a64f54 Kernel: Map interrupt handlers for all processors
This doesn't mean that processors will actually handle the irqs
2024-03-09 23:50:57 +02:00
Bananymous 2420886c2c Kernel: Move current and idle thread to Processor 2024-03-08 23:39:29 +02:00
Bananymous e636dce919 Kernel: Rewrite scheduler thread lists
Scheduler now has its own data SchedulerQueue which holds active nad
blocking thread lists. This removes need for BAN/Errors.h and making
current thread separate element instead of iterator into linked list.
This makes it possible to have current_thread on each processor
instead of a global one in Scheduler.
2024-03-08 22:13:45 +02:00
Bananymous 1a1f9b1cf2 Kernel: Fix {read,write}_gs_sized input operands to work always 2024-03-08 22:12:33 +02:00
Bananymous 54d0cb47cd BAN: Update ASSERT_NOT_REACHED message 2024-03-08 22:11:39 +02:00
Bananymous 23a2f8b903 Kernel: Cleanup multiprocessor startup by removing magic numbers 2024-03-07 17:01:17 +02:00
Bananymous 29fd682672 Kernel: Store current processor pointer in IA32_GS_BASE
This allows easier access to processors fields
2024-03-07 16:05:29 +02:00
Bananymous efed67cbd0 BAN: Remove unnecessary default constructor from Array 2024-03-06 16:01:52 +02:00
Bananymous 6234a5bc0b Kernel: Move multiprocessor initialize after framebuffer is created
This allows getting output before multiprocessor is started.
2024-03-06 16:00:19 +02:00
Bananymous 54f64e7618 Kernel: Move current page table to Processor
APs can now map kernel page table and print current time!
2024-03-06 02:19:59 +02:00
Bananymous f0105cb7fb Kernel: Move Interruptable from InterruptController.h to its own file 2024-03-06 00:47:02 +02:00
Bananymous 76b0f80169 Kernel: Move IDT to Processor 2024-03-06 00:45:54 +02:00
Bananymous f84df175ce Kernel: Save BSB id 2024-03-06 00:36:09 +02:00
Bananymous 58aca68726 Kernel: Move GDT to Processor 2024-03-06 00:35:45 +02:00
Bananymous 8670364f44 BAN: Remove unnecessary include from Array.h 2024-03-06 00:01:06 +02:00
Bananymous 418bc54f2b Kernel: Move SpinLock definition to header and fix Scheduler locking
This patch allows inlining of spinlocks :)
2024-03-04 22:36:41 +02:00
Bananymous 9c36d7c338 BAN/Kernel: Rework assertion/panic system
BAN/Assert.h does not need any includes meaning it can be included
anywhere without problems.
2024-03-04 11:41:54 +02:00
Bananymous 8141b9977d Kernel: Per processor information is now stored in class Processor
This allows us to allocate processor stacks, and other per processor
structures dynamically in runtime. Giving processor stack to
ap_trampoline feels super hacky, but it works for now.
2024-03-03 22:30:06 +02:00
Bananymous c035d3c82c Kernel: Start all processors on kernel boot
Processors don't do anything, except print hello message and halt.
2024-03-03 02:19:43 +02:00
Bananymous 1de9daa40f Kernel: Move interrupt status stuff to Processor.h
SpinLocks are now locked with processor id instead of thread id. This
allows having multiple processors running while scheduler is not yet
activated.
2024-03-03 01:47:22 +02:00
Bananymous efd8203232 BAN: Atomic memory order can be set per function call 2024-03-03 01:41:46 +02:00
Bananymous a667d88f93 Kernel: Remove now obsolete CriticalScope
All critical scopes in kernel are now replaced by SpinLocks. This
allows proper locking in actual multiprocessing with multiple cores.
2024-03-01 15:51:02 +02:00
Bananymous 8d7dd577ab Kernel: Replace last CriticalScopes in kernel with SpinLocks 2024-03-01 15:49:39 +02:00
Bananymous 054b41383f Kernel: Implement SpinLock unsafe that does not keep track of locker
This will be used in scheduler where thread ids are changing
2024-03-01 15:48:08 +02:00
Bananymous 02ad199138 Kernel: Move interrupt status functions to kernel/Interrupts.h 2024-03-01 02:13:28 +02:00
Bananymous 65c4f9db5b Kernel: Replace CriticalScopes with SpinLocks in Process/Thread 2024-03-01 02:13:28 +02:00
Bananymous 51e38b7614 Kernel: Replace CriticalScope with SpinLock in SerialTTY 2024-02-29 19:17:28 +02:00
Bananymous 90878a7c2b Kernel: Replace CriticalScopes with SpinLocks in networking code 2024-02-29 19:17:28 +02:00
Bananymous 7f028f70d5 Kernel: Replace CriticalScopes with SpinLock in PS/2 and input code 2024-02-29 19:17:28 +02:00
Bananymous ec0cb5fd54 Kernel: Remove CriticalScopes from memory handing code 2024-02-29 19:16:41 +02:00
Bananymous 682de62c57 Kernel: Replace HPET CriticalScope with SpinLock 2024-02-29 19:15:33 +02:00
Bananymous 18253b6966 Kernel: Replace InterruptController CriticalScopes with SpinLock 2024-02-29 19:15:33 +02:00
Bananymous 21f05eb118 Merge branch 'main' into locking 2024-02-28 23:00:02 +02:00
Bananymous d94f6388b7 Kernel: Fix all broken locks from new mutexes 2024-02-28 22:45:34 +02:00
Bananymous 1971813336 BAN: Add HashMap::remove(iterator) 2024-02-28 22:35:42 +02:00
Bananymous 3c88d2aad3 BAN: Implement find() for HashMap 2024-02-28 13:20:24 +02:00
Bananymous 5c39903323 Kernel: Simplify writing to threads stacks
This can be done more simply as all stacks are now page aligned
2024-02-28 13:20:17 +02:00
Bananymous 6d59a2b45d Kernel: Remove VirtualRanges created in kmalloc memory
These were only used as stacks of kernel threads, but they can just
as well be allocated as full pages from Heap
2024-02-28 13:19:18 +02:00
Bananymous 09c24088a2 Kernel: Cleanup kmalloc VirtualRange creation 2024-02-28 13:19:18 +02:00
Bananymous efdc4817bb Kernel: Print to debug log ICMP unreachable messages
These messages should be forwarded to underlying sockets
2024-02-28 13:19:18 +02:00
Bananymous 0c97abb053 BAN: Implement find() for HashMap 2024-02-27 15:40:30 +02:00
Bananymous 1759d247d9 Kernel: Simplify writing to threads stacks
This can be done more simply as all stacks are now page aligned
2024-02-25 21:57:33 +02:00
Bananymous 21dc64dc21 Kernel: Remove VirtualRanges created in kmalloc memory
These were only used as stacks of kernel threads, but they can just
as well be allocated as full pages from Heap
2024-02-25 21:56:37 +02:00
Bananymous 264eff3ad0 Kernel: Cleanup kmalloc VirtualRange creation 2024-02-25 21:33:30 +02:00
Bananymous 05c7b21b0a Kernel: Print to debug log ICMP unreachable messages
These messages should be forwarded to underlying sockets
2024-02-25 21:32:37 +02:00
Bananymous 40b626b0aa Kernel: Rewrite all kernel mutexes
Now SpinLock is actually just a spin lock and I added a Mutex that
does the same as the old "SpinLock". This is in preparation for
starting to support smp and making the kernel smp safe. This commit
also removes obsolete PageTableScope and CriticalScope which should
now be used by alternative APIs.
2024-02-25 21:29:43 +02:00
Bananymous 6ebfe05fce BAN: Add parenthesis in ASSERT macros 2024-02-25 21:22:47 +02:00
Bananymous 59abb5d344 Kernel: Make HPET read_main_counter() atomic with 32 bit main counter 2024-02-23 13:42:04 +02:00
Bananymous 9594ee8e47 Kernel: Start making device numbers unique for each device 2024-02-22 15:53:48 +02:00
Bananymous 7a4ec7f7a3 Kernel: Use static_cast instead of c-style cast in HPET 2024-02-22 14:44:39 +02:00
Bananymous 51db1706e9 Kernel: Fix checking of partition boundaries on write 2024-02-22 13:31:12 +02:00
Bananymous ac9e71d9c7 LibC: Fix parsing mode from string 2024-02-20 13:25:24 +02:00
Bananymous f3f5ca1bd8 Kernel: Seed RNG by real time if no RDRAND available 2024-02-20 13:00:26 +02:00
Bananymous b979023b9d Shell: Add test cases for stroul and strod in builtin `test-strtox` 2024-02-16 15:35:02 +02:00
Bananymous 915dea01c9 LibC: fix printf %e for inf/nan values 2024-02-16 15:34:24 +02:00
Bananymous 566bb73897 LibC: Implement ato* and strto* functions for floating point numbers 2024-02-16 15:28:52 +02:00
Bananymous fb0d8d746f BAN: Add {max,min}_exponent{2,10} to numeric_limits<T> 2024-02-16 15:27:40 +02:00
Bananymous 1b24c4f279 LibC: Implement strtou{l,ll} 2024-02-15 12:25:56 +02:00
Bananymous a5a041e637 LibC: Remove cast from S_* macros so they can be used in preprocessor 2024-02-15 12:20:09 +02:00
Bananymous c469d9b3ff Shell: Add builtin test for strtol `test-strtol` 2024-02-14 22:38:16 +02:00
Bananymous 373d166076 LibC: Implement ato{i,l,ll} and strto{l,ll} 2024-02-14 22:35:23 +02:00
Bananymous 3c54243ac7 BAN: Implement some numerical limits 2024-02-14 22:34:42 +02:00
Bananymous 1f467580ee Userspace: Add test for popen 2024-02-14 17:23:26 +02:00
Bananymous 1ba883719a LibC: Implement popen and pclose 2024-02-14 17:22:45 +02:00
Bananymous f73e954b28 Kernel: Remove SpinLock from Pipe
Pipe already is using lock on the inode. If you read from pipe when
there was no data, pipe blocked indefinately since writes were blocked
by Inode::m_lock.
2024-02-14 17:21:32 +02:00
Bananymous de629291b9 LibC: Implement freopen, rewind and fix bugs in code
Now everything will be properly locked once threads are implemented.
All functions "lock" the stream for the wanted operation
2024-02-14 16:36:48 +02:00
Bananymous 7eb5d220fd Userspace: Implement getopt for testing libc getopt() 2024-02-14 15:01:27 +02:00
Bananymous 4cd9abdd15 LibC: Implement getopt() 2024-02-14 15:00:58 +02:00
Bananymous 198dde8365 Kernel: Add klibc for kernel
Now building same source as libc is not needed and libc doesn't
have to do hacks to allow kernel compilation
2024-02-14 15:00:04 +02:00
Bananymous b165340662 Kernel: Don't use strcat in kernel code 2024-02-14 14:59:13 +02:00
Bananymous 5ad4340679 BAN: Use strerrordesc_np instead of strerror 2024-02-14 14:58:27 +02:00
Bananymous b56fa4a29d LibC: Implement fscanf
I had missed this when I was implementing *scanf functions
2024-02-14 14:39:22 +02:00
Bananymous e946b392c9 LibC: Add definition for S_IFMT
I was using S_IFMASK, but linux seems to use this
2024-02-14 14:39:22 +02:00
Bananymous 81689b5f02 LibC: Implement most of missing functions from string.h
only strcoll*, strxfrm* and strerror_l are left unimplemented
2024-02-14 14:39:22 +02:00
Bananymous c18d926174 LibC: Fix timeval field name 2024-02-14 03:36:18 +02:00
Bananymous 00662bad46 Kernel: Rewrite HPET code
Now the set timer frequency actually works... :D
2024-02-13 17:59:48 +02:00
Bananymous 420a7b60ca resolver: use select for client communication 2024-02-12 23:47:39 +02:00
Bananymous 2ab3eb4109 Kernel: Fix bugs in select
Unix domain socket is now select readable when it has pending
connection
2024-02-12 23:46:27 +02:00
Bananymous 9314528b9b Kernel: Improve syscall handling
Syscalls are now called from a list of function pointers
2024-02-12 21:51:11 +02:00
Bananymous 78ef7e804f BAN: Implement bit_cast 2024-02-12 21:46:33 +02:00
Bananymous 3fc1edede0 Kernel/LibC: Implement super basic select
This does not really even block but it works... :D
2024-02-12 17:26:33 +02:00
Bananymous f50b4be162 Kernel: Cleanup TCP code 2024-02-12 15:44:40 +02:00
Bananymous ccde8148a7 Userspace: Implement basic udp test program 2024-02-12 04:45:42 +02:00
Bananymous b9bbf42538 Userspace: Implement basic test program for tcp connection 2024-02-12 04:45:42 +02:00
Bananymous 435636a655 Kernel: Implement super simple TCP stack
No SACK support and windows are fixed size
2024-02-12 04:45:42 +02:00
Bananymous ba06269b14 Kernel: Move on_close_impl from network socket to udp socket 2024-02-12 04:45:42 +02:00
Bananymous be01ccdb08 Kernel: Fix E1000 mtu 2024-02-12 04:25:39 +02:00
Bananymous b45d27593f Kernel: Implement super simple PRNG 2024-02-12 04:25:06 +02:00
Bananymous ff49d8b84f Kernel: Cleanup OSI layer overlapping 2024-02-09 17:05:07 +02:00
Bananymous 5d78cd3016 Kernel: Add spin lock assert back. I had accidentally deleted it 2024-02-09 16:58:55 +02:00
Bananymous ed0b1a86aa Kernel: Semaphores and Threads can now be blocked with timeout 2024-02-09 15:28:15 +02:00
Bananymous 534b3e6a9a Kernel: Add LockFreeGuard to LockGuard.h 2024-02-09 15:13:54 +02:00
Bananymous d452cf4170 Kernel: Fix checksum for packets with odd number of bytes 2024-02-09 01:20:40 +02:00
Bananymous f117027175 resolver: dump errors to debug output 2024-02-08 18:34:15 +02:00
Bananymous acf79570ef Kernel: Cleanup network APIs and error messages 2024-02-08 18:33:49 +02:00
Bananymous 5a939cf252 Userspace: Add simple test for unix domain sockets 2024-02-08 13:18:54 +02:00
Bananymous 9bc7a72a25 Kernel: Implement unix domain sockets with SOCK_DGRAM
Also unbind sockets on close
2024-02-08 13:18:54 +02:00
Bananymous 065ee9004c Userspace: Add DNS cache to resolver
Also the format of resolver reply is now just sockaddr_storage with
family set and address in the storage field.
2024-02-08 12:06:30 +02:00
Bananymous 6fb69a1dc2 LibC: Implement inet_ntop for IPv4 addresses 2024-02-08 11:59:11 +02:00
Bananymous 49889858fa Kernel: Allow chmod on TmpSocketInode 2024-02-08 03:16:01 +02:00
Bananymous 2424f38a62 Userspace: Implement super simple DNS resolver in userspace
You connect to this service using unix domain sockets and send the
asked domain name. It will respond with ip address or 'unavailable'

There is no DNS cache implemented so all calls ask the nameserver.
2024-02-08 03:14:00 +02:00
Bananymous 218456d127 BAN: Fix some includes 2024-02-08 03:13:21 +02:00
Bananymous e7dd03e551 Kernel: Implement basic connection-mode unix domain sockets 2024-02-08 02:28:19 +02:00
Bananymous 0c8e9fe095 Kernel: Add operator bool() for WeakPtr 2024-02-08 02:26:46 +02:00
Bananymous 5b4acec4ca BAN: Add capacity() getter for Queue 2024-02-07 22:53:56 +02:00
Bananymous e26f360d93 Kernel: allow kmalloc of size 0 2024-02-07 22:36:24 +02:00
Bananymous 2cc9534570 BAN: Add emplace for Variant
This allows variant to store values that are not copy/move
constructible.
2024-02-07 22:33:16 +02:00
Bananymous 572c4052f6 Kernel: Fix Process APIs 2024-02-07 15:57:45 +02:00
Bananymous 132286895f Kernel: Implement Socket inodes for tmpfs 2024-02-07 15:57:45 +02:00
Bananymous 454bee3f02 LibC: Fix sockaddr_un implementation 2024-02-07 15:57:45 +02:00
Bananymous 41cad88d6e Kernel/LibC: Implement dummy syscalls for accept, connect, listen 2024-02-07 15:57:45 +02:00
Bananymous 40e341b0ee BAN: Remove unstable hash map and set
These can now be implemented safely with new linked list api
2024-02-06 17:35:15 +02:00
Bananymous 5da59c9151 Kernel: Make better abstractions for networking 2024-02-06 16:45:39 +02:00
Bananymous f804e87f7d Kernel: Implement basic gateway for network interfaces 2024-02-05 18:18:56 +02:00
Bananymous dd3641f054 Kernel: Cleanup ARPTable code
Packet process is now killed if ARPTable dies.

ARP wait loop now just reschecules so timeout actually works.
2024-02-05 18:18:56 +02:00
Bananymous b2291ce162 Kernel/BAN: Fix network strucute endianness 2024-02-05 18:18:56 +02:00
Bananymous c35ed6570b LibC: Implement endiannes and ip address functions 2024-02-05 18:18:56 +02:00
Bananymous d15cbb2d6a Kernel: Fix IPv4 header checksum calculation 2024-02-05 18:18:56 +02:00
Bananymous b8cf6432ef BAN: Implement host_to_network_endian 2024-02-05 17:29:24 +02:00
Bananymous 89805fb092 dhcp-client: Use dprintln for debug printing 2024-02-05 01:24:45 +02:00
Bananymous 692cec8458 Kernel/Userspace/LibC: Implement basic dprintln for userspace 2024-02-05 01:24:09 +02:00
Bananymous 79897e77dc dhcp-client: Remove packet send test :D 2024-02-03 18:04:31 +02:00
Bananymous 649e9f4500 Kernel: ARP now replies to requests 2024-02-03 18:04:12 +02:00
Bananymous 3a6d31d3fa BAN: Add comparison for MAC 2024-02-03 18:03:27 +02:00
Bananymous 2138eeb87f Userspace: Implement super simple DHCP client 2024-02-03 02:41:06 +02:00
Bananymous 102aa50a41 BuildSystem: Use E1000E network controller in qemu 2024-02-03 02:40:15 +02:00
Bananymous 5cfe249945 Kernel: Cleanup network code and implement basic ARP request 2024-02-03 02:39:26 +02:00
Bananymous a0138955cd Kernel: Implement barebones arp table 2024-02-03 01:50:10 +02:00
Bananymous e1ffbb710b Kernel/LibC: Implement basic ioctl for network addresses 2024-02-03 01:50:10 +02:00
Bananymous c18f72ceb9 BAN: Add more APIs for IPv4 address 2024-02-03 01:50:10 +02:00
Bananymous bc1441a5eb LibC: add stropts.h 2024-02-02 14:29:20 +02:00
Bananymous 0f154c3173 Kernel: Implement basic recvfrom 2024-02-02 13:50:00 +02:00
Bananymous 7b287a1d5b BAN: Add types for IPv4 and MAC addresses 2024-02-02 13:48:07 +02:00
Bananymous 4b332b5d42 Kernel: Cleanup PCI code 2024-02-02 03:16:37 +02:00
Bananymous ec2f21bb9f Kernel/LibC: Implement SYS_SENDTO 2024-02-02 03:16:01 +02:00
Bananymous acd6c86f98 BAN: Add NetworkEndian to Endianness 2024-02-02 03:13:14 +02:00
Bananymous ab150b458a Kernel/LibC: Implement basic socket binding 2024-02-02 01:31:58 +02:00
Bananymous cf28ecd5a6 Kernel/LibC: Add SYS_SOCKET 2024-02-01 23:39:09 +02:00
Bananymous 99eed9c37a Kernel: Start work on network stack 2024-02-01 23:38:06 +02:00
Bananymous f4e86028d0 Kernel: Write simple working E1000 and E1000E drivers 2024-02-01 22:08:59 +02:00
Bananymous 7cb71ec6fb test-sort: Test more algorithms and cleanup output format 2024-02-01 15:22:28 +02:00
Bananymous d054e5b4b7 BAN: Implement basic radix sort for unsigned integers 2024-02-01 15:22:28 +02:00
Bananymous c69efc040c Kernel: Scheduler now uses the new LinkedList API for moving threads
Scheduler doesn't have to depend on the fact that allocations should
work when same amount of memory is just deallocated
2024-02-01 15:22:28 +02:00
Bananymous c4bf1641bd BAN: Add cool API for LinkedList
You can now move elements between LinkedLists without allocations or
deallocations. Same node moves from source to destination
2024-02-01 14:19:02 +02:00
Bananymous 9213dd13bc dd: Improve output format
Include written bytes and speed with units
2024-02-01 00:06:46 +02:00
Bananymous 4273f43be1 BAN: Move placement new to its own file and fix includes 2024-01-31 23:55:41 +02:00
Bananymous 139bb5c2a5 Kernel: Fix linker script
I have no idea why rodata was executable :D
2024-01-30 17:32:50 +02:00
Bananymous 95e861bcdd Kernel: Optimize all SpinLocks. All locking operations are atomic 2024-01-30 12:39:37 +02:00
Bananymous ca8e7b40bc Kernel: Implement SpinLock without CriticalScope
This actually is not even spinlock since it yields the current
thread. It will become one when I get to SMP though...
2024-01-30 01:21:15 +02:00
Bananymous cc79f55817 BAN: Add compare_exchange to atomic 2024-01-30 01:21:15 +02:00
Bananymous a1faa836c5 Kernel: Hack NVMe controller to work :) 2024-01-30 01:06:00 +02:00
Bananymous 1f8aaa6fba Toolchain: Add `insmod all_video` to uefi grub
This allows getting GOP framebuffer... This was all it took
2024-01-26 00:50:04 +02:00
Bananymous 6bfe833aa5 Kernel: Parse RSDP from multiboot headers if exists 2024-01-26 00:49:42 +02:00
Bananymous 0408aa9bbc BAN: Implement is_unsigned* traits and (un)?signed_integral concepts 2024-01-25 14:17:23 +02:00
Bananymous 210b24b6e3 AOC2023: Delete file that should not be committed... 2024-01-24 15:53:38 +02:00
Bananymous dfe5a2d665 All: Cleanup all files
Add newline to end of files and remove whitespace from end of lines
2024-01-24 15:53:38 +02:00
Bananymous 3441f63298 Kernel: Don't panic kernel if no interrupt could be reserved 2024-01-24 14:34:59 +02:00
Bananymous 2cee2a85e6 Kernel: DevFS now stores all devices
This allows removing hack in PCI that was required to keep NVMe
controller alive.
2024-01-24 14:33:50 +02:00
Bananymous 5001fa58e0 Kernel: Fix wait syscall with atomics 2024-01-24 14:32:52 +02:00
Bananymous d2cf7c7a5c BAN: Implement basic Atomic class that wraps gcc builtins 2024-01-24 14:30:04 +02:00
Bananymous e544e6a62d LibC: Implement floating point parsing to *scanf functions 2024-01-24 11:54:12 +02:00
Bananymous 606a7cb313 LibC: Implement almost POSIX compliant *scanf functions
Only wchar strings and floating point values are not parsed
2024-01-24 10:33:12 +02:00
Bananymous de4fdcd898 BAN: Implement is_base_of and integral_constant to Traits 2024-01-22 16:59:14 +02:00
Bananymous 2c471a89d0 LibC: Add compile option to libc to stop optimizing string.h 2024-01-17 19:44:29 +01:00
Bananymous a5660b95b2 BAN: String don't memcpy with nullptr 2024-01-17 19:04:40 +01:00
Bananymous 54a92293da Kernel: Implement NVMe driver
I'm  actually able to boot this os fine on own laptop now!
2024-01-17 08:26:58 +01:00
Bananymous 812e9efd41 Kernel: StorageDevices now specify prefix for partition names 2024-01-14 01:16:48 +02:00
Bananymous c6130f33d7 Kernel: Implement MSI, MSI-X and interrupt reservation 2024-01-13 18:21:21 +02:00
Bananymous 56a29dc176 Kernel: Fix PS/2 Controller if port 0 is empty 2024-01-13 17:05:29 +02:00
Bananymous 7e36a0be75 Bootloader: Add .data section 2024-01-12 19:27:36 +02:00
Bananymous 7adc7e55a5 Kernel: Fix timeouts in AHCI code and add more volatile keywords 2024-01-12 19:26:20 +02:00
Bananymous 4be726b130 Kernel: Implement more error handling in IDE controller 2024-01-12 02:55:06 +02:00
Bananymous ff2486f58c Bootloader: Try to enable A20 line if it is disabled
VirtualBox seems to have A20 disabled by default
2024-01-12 02:55:06 +02:00
Bananymous db933d5466 Kernel: Improve keymap file loading
Now you can include other files in keymaps and set which keys are
modifier keys

Only keys that are set in keymap file are actually updated
2024-01-12 02:55:06 +02:00
Bananymous 8e31ab2de8 BuildSystem: clean target now deletes disk image 2024-01-11 13:27:02 +02:00
Bananymous 83ca469ed7 Kernel: Modifier keys are taken from current keyboard layout
I used to assume where all modifiers were, but they are now taken
from keyboard layout.
2024-01-11 11:53:11 +02:00
Bananymous d2c0718f7d Kernel: Fix toggleable modifier keys and add two more keys 2024-01-11 11:43:05 +02:00
Bananymous c1f0704fa8 fi keymap: Fix arrow key keycodes 2024-01-10 14:59:50 +02:00
Bananymous 64a63fa4be Userspace: Add loadkeys program to change keymap 2024-01-10 14:57:50 +02:00
Bananymous ab39c6541a Kernel: Require keymap loading superuser privileges 2024-01-10 14:50:30 +02:00
Bananymous 51214ea1bf Kernel: Add load_keymap syscall and load Finnish keymap in init 2024-01-10 14:46:29 +02:00
Bananymous 8f89519bcf Kernel: Keymaps can now be loaded from files 2024-01-10 14:43:19 +02:00
Bananymous e6d42e5c45 Kernel: Add timeout for device commands on PS/2 devices 2024-01-10 12:51:24 +02:00
Bananymous 961ab9768a Kernel: KeyEvent is now well known keycode
Keycodes are easier to handle as you need only one keyboard layout
for keycodes. Otherwise you would need to implement keyboard layout
for every keyboard driver in every language.
2024-01-10 12:51:24 +02:00
Bananymous e4f48cbc73 Kernel: Move PS/2 command queue to controller instead of device 2024-01-09 20:00:19 +02:00
Bananymous e8f853a197 Kernel: After device updates, reschedule instead of sleeping 2024-01-09 11:21:00 +02:00
Bananymous d760239748 Kernel: Do a big rewrite of PS/2 code
Command sending+response reading is now synchronized. This allows
bochs to properly initialize both mouse and keyboard simultaneously.

Also status register is checked EVERY time read/write to other IO
ports is performed.
2024-01-08 10:50:55 +02:00
Bananymous 2fec718590 Bootloader: cleanup code 2024-01-08 10:50:55 +02:00
Bananymous f0cf54e194 Kernel: Reads from PS/2 keyboard and mouse can be terminated 2024-01-05 12:13:44 +02:00
Bananymous 41ae05dd6e Kernel: Update block_or_eintr API to return ErrorOr<> 2024-01-05 12:13:11 +02:00
Bananymous 40f55be587 Userspace: Write simple mouse test program
This program draws circle on framebuffer, that you can move with
mouse, resize with scroll, and recolor with mouse buttons.
2024-01-04 19:50:26 +02:00
Bananymous 54c811ac2e Kernel: Fix some mouse scrolling bugs and cleanup event generation 2024-01-04 19:50:26 +02:00
Bananymous 12a78c822e Kernel: Explicitly construct ByteSpan from Span<uint8_t>
VSC complains about not finding proper constructor, this fixes that.
2024-01-04 12:17:55 +02:00
Bananymous e45b544a39 Kernel: Implement PS/2 mouse driver
This is realtively simple driver that queries extensions (scroll +
extra buttons) from mouse and reads mouse packages.
2024-01-04 12:17:55 +02:00
Bananymous d1e187570e Kernel: Fix old keyboard command 2024-01-04 12:08:23 +02:00
Bananymous d4191c0d94 Kernel: Reorganize PS/2 files to their own directory 2024-01-04 12:04:45 +02:00
Bananymous c2957d8761 Kernel: PS/2 device automatically sends commands when appended 2024-01-04 12:04:45 +02:00
Bananymous 891ced4da2 Kernel: Move PS2Device to its own file 2024-01-04 12:04:45 +02:00
Bananymous 8f8d6bddc0 Kernel: Unify PS2Device to handle commands instead of inherited 2024-01-04 12:04:45 +02:00
Bananymous d2d12d5281 Kernel: validate_{string,pointer}_access now return ErrorOr<void>
Now that signals are only processed when returning to userspace,
address validation has to do an early return.
2024-01-03 23:53:04 +02:00
Bananymous 0ba278041b Kernel: Start exec by loading the ELF file. It might not exist... 2024-01-03 23:50:02 +02:00
Bananymous ccaa159a73 Bootloader: Add support for ext2 blocks up to 4 KiB
This should work with blocks bigger than that, but my linux system
only supports up to 4 KiB, so I cannot test this.

This allows getting rid of forced block size in mkfs and let the
program select appropriately sized blocks.
2024-01-03 18:26:03 +02:00
Bananymous 7356a83a44 Bootloader: Optimize some unnecessary branches on carry add/sub 2024-01-03 17:05:33 +02:00
Bananymous 2a68df81e2 Bootloader: Fix bootloader on bochs
Bochs int 0x10 seems to scrap full ebp and top bits in some
registers. I now save all 32-bit registers on call frame. Also
ebp is across all int 0x10 calls.
2024-01-03 13:09:55 +02:00
Bananymous 50ca2ac09e Bootloader: Interpret 0x7F (DEL) as backspace
This allows backspace to work over qemu's serial connection
2024-01-03 11:58:50 +02:00
Bananymous b0ff2392a1 Kernel: Add some helpful debug prints
Log RIP when stack pointer is out of bounds.

Log all syscalls that return ENOTSUP
2024-01-03 02:08:01 +02:00
Bananymous 001e95f973 Kernel: Optimize sse saving/loading
Only save and load sse when new thread is trying to execute sse
instruction. There is no need to do that every time we enter kernel.
2024-01-03 02:06:49 +02:00
Bananymous db0650cf10 LibC: Implement basic atexit. This allows clean exit from doom (soon) 2024-01-03 00:30:37 +02:00
Bananymous 753de3d9f0 ls: Write group name instead of gid in list mode 2024-01-03 00:15:13 +02:00
Bananymous 668c4c8976 LibC: Implement getgrnam and getgrgid 2024-01-03 00:14:49 +02:00
Bananymous d2bc399770 BAN: Make StringView::split const and fix bug with empties 2024-01-03 00:14:29 +02:00
Bananymous 1bd33e76e5 cat/cat-mmap: print newline if file doesn't end in one 2024-01-02 23:27:13 +02:00
Bananymous 9fa13079f2 Kernel: Implement supplementary groups
This code has very ugly file parsing code. I have to create API
for reading files line by line in kernel space...

This allows users to open framebuffer/input files without root.

Mounting has to be moved to userspace soon. It makes no sense to
hard code permissions for every (device) file.
2024-01-02 23:24:32 +02:00
Bananymous 96d831c31a Kernel/LibC/Userspace: Implement chown and set tty owner on login 2024-01-02 22:19:38 +02:00
Bananymous 07d5d3f936 BAN: Simple iterator now contains valid flag
This allows iteration over empty containers
2023-12-29 14:49:57 +02:00
Bananymous 51820b15cb README: Add packages required by compilation in pacman 2023-12-29 03:11:30 +02:00
Bananymous 9fafafb17e BuildSystem: Add instructions + fixes to build system
You can now easily build with clean ubuntu install
2023-12-28 21:37:18 +02:00
Bananymous 7c6565880d BuildSystem: Use git apply instead of am
This allows you to not have configured git user
2023-12-28 20:16:36 +02:00
Bananymous 93a72ebd06 BuildSystem: Download cmake only if needed 2023-12-28 19:50:22 +02:00
Bananymous 4307968182 All: Start work again on sse support 2023-12-28 19:14:42 +02:00
Bananymous 5d83ab2289 BuildSystem: Download correct cmake if it is not available 2023-12-28 19:13:27 +02:00
Bananymous af80bad87a BuildSystem: Allow running custom version of cmake
You can now set the environment variable CMAKE_COMMAND to use custom
cmake version.
2023-12-27 13:01:11 +02:00
Bananymous 87272f0cd7 BuildSystem: Create build directory if it doesn't exists 2023-12-27 12:36:40 +02:00
Bananymous 8b5e437936 AOC2023: Implement day25 part1
7 starts missing + partly broken day22. I might finish these soon...
2023-12-26 20:04:42 +02:00
Bananymous 3939da4fb0 BuildSystem: Increase image size 50MiB -> 500 MiB 2023-12-26 15:01:12 +02:00
Bananymous d87fa1a7ea AOC2023: Implement day24 part1 2023-12-26 14:10:49 +02:00
Bananymous 910a57089b AOC2023: Implement day23 part1
My day22 implementation is off by couple of bricks...
2023-12-26 00:56:35 +02:00
Bananymous 861bf27e96 BAN: Implement abs() 2023-12-26 00:54:06 +02:00
Bananymous 36590fb5c7 AOC2023: Implement day21 part1 2023-12-24 14:58:21 +02:00
Bananymous ce990c3026 AOC2023: Implement day20 part1 2023-12-24 13:39:26 +02:00
Bananymous b833239a82 BAN: Make hashmap work with non-copy constructable values 2023-12-24 13:38:47 +02:00
Bananymous 6fec142760 BAN: Add requires clauses for vector 2023-12-24 13:38:20 +02:00
Bananymous 84b2438b3d BAN: Add requires for copy constructor for linked list 2023-12-24 13:37:49 +02:00
Bananymous 0e714d5eb4 BAN: Implement String::operator==(const String&) 2023-12-24 13:36:46 +02:00
Bananymous 9b8e6e6629 BAN: Implement is_*constructable 2023-12-24 13:36:12 +02:00
Bananymous 4146f2777b AOC2023: Implement 10 hour solution to day19 part2 2023-12-23 20:53:50 +02:00
Bananymous 64323c51e6 AOC2023: Implement day19 part 1 2023-12-23 18:47:44 +02:00
Bananymous a0200a7b10 AOC2023: Implement day18 part 1 2023-12-23 18:47:21 +02:00
Bananymous 8add759b5d AOC2023: Implement day17 part1 2023-12-23 18:46:43 +02:00
Bananymous 2faf90bc2b AOC2023: Add script to create day template and download input 2023-12-23 18:46:14 +02:00
Bananymous 762d575d70 AOC2023: Add program to run all days 2023-12-23 18:45:40 +02:00
Bananymous 2e77718f07 BAN: Implement find() for StringView 2023-12-23 18:43:52 +02:00
Bananymous f371fabe35 BAN: HashSet is now internally Vector<LinkedList<T>>
It used to be Vector<Vector<T>> but this feels more proper
2023-12-23 16:37:21 +02:00
Bananymous 79a15132da BAN: Cleanup HashSet
I now use BAN::Iterator as the base iterator. Also unstable version
was added.
2023-12-23 16:32:06 +02:00
Bananymous bacc0db778 BAN: Fix unstable hash map rebucket 2023-12-23 16:31:42 +02:00
Bananymous 3963afe343 BAN: Add unstable version of hash map
This version differs only when doing rebucket. If rebucket fails,
the whole hash map is invalidated. This allows rebucketing to use
moving instead of copying.
2023-12-19 22:23:28 +02:00
Bananymous 3b21cc90ae AOC2023: Add helper for downloading puzzle input 2023-12-19 22:22:31 +02:00
Bananymous 4e900804b8 AOC2023: Implement day16 2023-12-19 22:22:16 +02:00
Bananymous 9ec733904f AOC2023: Implement day15 2023-12-19 21:42:59 +02:00
Bananymous 3352640d09 LibC: strlen had to be marked not optimized... 2023-12-19 21:42:59 +02:00
Bananymous 637397dd2f LibC: Make memcpy and memset not optimized
GCC does some weird optimizations and breaks these functions
2023-12-19 21:42:59 +02:00
Bananymous 5edbb1d5c4 LibC: make execvp fail if no executable found 2023-12-19 21:42:59 +02:00
Bananymous f46240e879 AOC2023: Implement day14 2023-12-19 02:49:48 +02:00
Bananymous 68627995f8 AOC2023: Implement day13 2023-12-19 01:25:23 +02:00
Bananymous 951eac6bfa Kernel: Implement hacky non-block read for ps2 keyboard 2023-12-19 00:20:46 +02:00
Bananymous 0833d7b43f Kernel: Allow opening files with O_NONBLOCK 2023-12-19 00:20:15 +02:00
Bananymous 1cd5b3c20c LibC: Fix stpncpy
I had misunderstood and tought that the string is always
null terminated
2023-12-15 00:33:37 +02:00
Bananymous c773e2ed07 LibC: Optimize malloc even further
aoc2023/day12 now runs in 3.5 seconds on my machine. This is way
better than the old almost hour.
2023-12-14 23:49:25 +02:00
Bananymous c4186bd5f0 LibC: Compile with -O2 optimizations
I have no idea why libc had no optimizations enabled.

Weird thing is that memcpy optimized to infinite loop if I kept the
__restrict__ attributes in pointers. I don't think there was any ub.
2023-12-14 23:40:08 +02:00
Bananymous 5f640da166 LibC: Optimize malloc by a lot
I now cache first free node in malloc_pool and whether the node is
last or not. There allow doing less full iterations over the whole
malloc pool.

Malloc is still unbearably slow and I will have to write a proper
fast malloc at some point. With this patch running aoc2023/day12 is
atleast possible. Probabaly will take closer to an hour...
2023-12-14 15:31:00 +02:00
Bananymous 7320104fd0 LibC: Mark __assert_fail as noreturn 2023-12-14 15:13:54 +02:00
Bananymous d273c5e77c rm: Use remove instead of unlink for removing files 2023-12-14 11:03:58 +02:00
Bananymous 8344f2f9ab AOC2023: Fix day3 implementation
I accidently broke my day3 when messing with signed/unsigned integers
2023-12-14 11:03:19 +02:00
Bananymous 600bd7ee0f LibC: Implement rmdir in unistd.h 2023-12-14 11:02:56 +02:00
Bananymous adf1e54605 LibC: Implement more functions to string.h 2023-12-14 11:02:30 +02:00
Bananymous a3de64f5fa LibC: Implement basic version of system() this assumes Shell exists 2023-12-14 11:00:40 +02:00
Bananymous 8216d09e06 LibC: Implement non-locale specific functions from strings.h 2023-12-14 10:59:39 +02:00
Bananymous 694cda6e40 LibC: Implement remove for stdio 2023-12-14 10:58:50 +02:00
Bananymous e227a87140 Kernel: Allow creating directories if path ends with '/'
Also create and create_dir will now fail with EEXISTS if file exists
2023-12-14 10:56:53 +02:00
Bananymous 6cd5763361 Kernel: Allow cloning of mmapped framebuffer regions 2023-12-14 10:54:06 +02:00
Bananymous 0f1c740fe8 Kernel: Implement two missing ubsan handlers needed by lai 2023-12-14 10:53:36 +02:00
Bananymous 3f3e81fcf2 Bootloader: Align boot information passed to kernel
UBSAN found this bug
2023-12-14 10:52:51 +02:00
Bananymous 862993398d AOC2023: Implement day12
There seems to be a problem with my malloc so the hash map is not
working. This code worked fine on my linux with actually working
malloc :D
2023-12-14 10:51:48 +02:00
Bananymous 225c7c6ab4 AOC2023: Implement day11 2023-12-11 14:36:33 +02:00
Bananymous 58633ca373 AOC2023: Remove unnecessary loop 2023-12-10 20:18:59 +02:00
Bananymous 00d57d783e LibC+userspace: Make everything compile with -Wall -Wextra -Werror
I added -Wall -Wextra -Werror as public compile flags to libc. Now
everything in userspace in compiled using these flags. I made all
necessary changes to allow compilation to work.

Only exception is execvp which has a large stack usage. Maybe it
should use malloc for the buffer but posix allows ENOMEM only when
kernel is out of memory... This can be fixed when fexecve is
implemented and there is no need for absolute path.
2023-12-10 19:20:14 +02:00
Bananymous f077e17b2a AOC2023: Implement day10 2023-12-10 18:28:04 +02:00
Bananymous 2f8759d2d3 Kernel: Make ext2 fs work with block sizes != 1024 2023-12-10 01:32:30 +02:00
Bananymous a6bfbbf655 ls: Rewrite whole program for cleaner output
ls -l now sorts elements and aligns them by columns.
2023-12-10 01:05:12 +02:00
Bananymous 9d8c9baa3f LibC: Remove unnecessary RWX mask definition 2023-12-09 19:43:49 +02:00
Bananymous c273bf98c9 ls+stat: show setuid, setgid and sticky bits 2023-12-09 19:43:25 +02:00
Bananymous 99a5b6e2ef AOC2023: Cleanup day9 code
Move tree building to its own function. Both parts can use the same
tree. This also decreaseas memory usage by one element by row :D
2023-12-09 19:08:13 +02:00
Bananymous 284a012509 Kernel: Add framebuffer information to kernel image 2023-12-09 17:33:58 +02:00
Bananymous abc69fa3d5 Bootloader: Search framebuffer information from kernel memory
Framebuffer information is no longer hard coded into bootloader.
Kernel can define framebuffer info structure in its memory which is
used for finding proper video mode.
2023-12-09 17:32:10 +02:00
Bananymous 8b01e2d4a2 Bootloader: Generalize framebuffer video mode search
Framebuffer size is now taken as arguments to vesa_find_video_mode
2023-12-09 16:48:04 +02:00
Bananymous 0c3e5980d6 AOC2023: Implement day9 2023-12-09 16:22:43 +02:00
Bananymous 951873098e BAN: Rewrite heap sort
Heap sort is now more close to gnulibc++ version. This is just more
simplified version.
2023-12-08 22:45:55 +02:00
Bananymous 2b927b9729 BAN: Restructure sort functions and namespaces 2023-12-08 18:58:47 +02:00
Bananymous b523ccb893 AOC2023: Implement day8
Second part was kinda weird. You are supposted to assume something
of the input, which necessarily is not true.
2023-12-08 17:13:20 +02:00
Bananymous 7bb3172591 AOC2023: Use the default sort algorithm 2023-12-08 00:10:59 +02:00
Bananymous be657b9b18 BAN: Add default sort. This is wrapper around sort_intro 2023-12-08 00:10:09 +02:00
Bananymous 94e6b9fa65 BAN: Implement intro sort 2023-12-07 23:56:11 +02:00
Bananymous d8ea0eeba3 BAN: Add less than operator for iterator 2023-12-07 23:55:41 +02:00
Bananymous 6873244169 BAN: Move placement new to New.h
I have no idea why they were defined in Move.h
2023-12-07 23:52:57 +02:00
Bananymous 805b4096e9 BAN: Remove empty else. Builds with -Wall -Wextra 2023-12-07 23:50:35 +02:00
Bananymous 0f74e123b8 BAN: Implement ilog2 for unsigned integers 2023-12-07 23:50:04 +02:00
Bananymous 7f212106db BAN: Implement heap sort 2023-12-07 23:18:49 +02:00
Bananymous 46c3da71b6 BAN: Cleanup sorting code 2023-12-07 23:18:49 +02:00
Bananymous e5cab047d6 BAN: Implement more methods for iterators 2023-12-07 23:18:49 +02:00
Bananymous bf3e9eabd5 BAN: Implement distance() for iterators 2023-12-07 23:18:49 +02:00
Bananymous 19604015de BAN: Implement quick sort 2023-12-07 19:28:31 +02:00
Bananymous 08bc0a2815 BAN: Implement next() and prev() for iterators and use them in sorts 2023-12-07 19:28:05 +02:00
Bananymous 3bc7113cc5 sudo: fix some typos 2023-12-07 14:05:17 +02:00
Bananymous 24243268a6 Shell: do path resolution only if command doesn't contain '/' 2023-12-07 13:34:46 +02:00
Bananymous 2e858fddb5 Kernel: Remove obsolete Scheduler::is_valid_tid()
This function was used when processes could die at any point in time.
Now that processes can only die in known spots, we can be sure they
are not holding any locks. This allows much more performant locking.
2023-12-07 13:26:42 +02:00
Bananymous 12474addda Kernel: Make Inodes use the new lock
Also remove old lock from TTY since it can just use the one Inode
already has.
2023-12-07 13:19:12 +02:00
Bananymous 7c25e4ce5a Kernel: Implement RecursivePrioritySpinLock
This locks won't allow locking from userspace thread if there is
kernel thread waiting to lock this.
2023-12-07 13:18:21 +02:00
Bananymous 669d55707e AOC2023: Use quick sort in solution 2023-12-07 11:55:28 +02:00
Bananymous 6caa9b6f95 BAN: implement quick sort and test for it 2023-12-07 11:55:28 +02:00
Bananymous 59ad639fa8 BAN: Fix simple iterator operator--() 2023-12-07 11:55:28 +02:00
Bananymous 43458cc74f BAN: implement exchange sort and test for it 2023-12-07 11:55:28 +02:00
Bananymous e935a33a4d BAN: add value_type to iterators 2023-12-07 10:15:18 +02:00
Bananymous 536bb74d53 AOC2023: optimize hand score calculation 2023-12-07 09:26:17 +02:00
Bananymous a872efdef2 BAN: implement basic swap
This will be improved, currently just works on general type T and
moves values between arguments.
2023-12-07 09:26:17 +02:00
Bananymous efd8be8207 AOC2023: implement day7 2023-12-07 07:56:56 +02:00
Bananymous 06a84da844 Userspace: Implement barebones sudo
This doesn't do any password checking or anything. Just sets uid and
gid before execvp()
2023-12-06 18:15:42 +02:00
Bananymous 24b71d1170 Kernel: Appreciate setuid and setgid bits executables 2023-12-06 18:14:27 +02:00
Bananymous 0dc168a8c0 LibC: Implement basic execvp 2023-12-06 18:14:00 +02:00
Bananymous 76049b2e13 LibC: Implement and fix some string.h functions 2023-12-06 18:13:34 +02:00
Bananymous 393ac33e3c Kernel: Make sys_nanosleep interruptable by signals 2023-12-06 16:22:24 +02:00
Bananymous deeb6d2756 Kernel: Cleanup sys_sleep() and TTY::read_impl 2023-12-06 16:21:22 +02:00
Bananymous 1ac831d4b1 Kernel: Add API to block on semaphore until unblock or EINTR 2023-12-06 16:13:07 +02:00
Bananymous 534969df32 Kernel: Scheduler::unblock_thread() wakes sleeping threads 2023-12-06 16:12:37 +02:00
Bananymous 976ae64f88 LibC: make sleep() set errno if sleep woke up early 2023-12-06 13:13:43 +02:00
Bananymous a12ffaa8a2 Kernel: Make sleep syscall interruptable 2023-12-06 13:13:37 +02:00
Bananymous ff8b3be8dc Userspace: implement basic sleep command 2023-12-06 13:05:53 +02:00
Bananymous 56008869d6 Shell: Handle keyboard input that was interrupted by signal 2023-12-06 13:04:33 +02:00
Bananymous 1c78671078 Kernel: Rework all signal code
Signal handling code was way too complex. Now everything is
simplified and there is no need for ThreadBlockers.

Only complication that this patch includes is that blocking syscalls
have to manually be made interruptable by signal. There might be some
clever solution to combat this is make this happen automatically.
2023-12-06 13:02:17 +02:00
Bananymous cdcc36efde Kernel: Remove unnecessary raise syscall 2023-12-06 13:00:45 +02:00
Bananymous 336daa2cc5 Kernel: Add helper for checking whether segment is from userspace 2023-12-06 12:57:13 +02:00
Bananymous 531211e09d AOC2023: implement day6 2023-12-06 07:27:25 +02:00
Bananymous 894065a67e LibC: cleanup and fix fgets
My quick fix for fgets wrote non-nullterminated newline if size was
one.

POSIX doesn't specify what happens if size == 0, so I do the same as
glibc and return NULL without setting errno.
2023-12-05 10:00:43 +02:00
Bananymous 82cb2ea20b AOC2023: implement day5 2023-12-05 09:13:13 +02:00
Bananymous 95fc894303 LibC: fix fgets when reading empty line 2023-12-05 07:46:14 +02:00
Bananymous 622007f2ee Shell: Verify that command exists before executing it
This prevents page fault somewhere when executing non-existing
commands.
2023-12-04 22:57:27 +02:00
Bananymous cf76d2e7d9 AOC2023: remove unnecessary loop 2023-12-04 17:54:10 +02:00
Bananymous 41f8974080 AOC2023: implement day4 2023-12-04 17:51:27 +02:00
Bananymous 3ed25425a3 AOC2023: implement day3
Solution to second puzzle is not optimal, it definately should be
O(1) space, but I didn't want to think about it.
2023-12-03 17:07:09 +02:00
Bananymous 49f8c4268f AOC2023: implement day2 2023-12-02 16:10:37 +02:00
Bananymous 70c224d8ea AOC2023: Cleanup day1 code 2023-12-01 13:31:55 +02:00
Bananymous 6a7335e5c9 AOC2023: implement day1 2023-12-01 12:38:01 +02:00
Bananymous 3aaa755c51 AOC2023: update buildsystem 2023-12-01 12:38:01 +02:00
Bananymous c140dd2a65 Kernel: Fix keyboard layout for underscore 2023-12-01 12:31:16 +02:00
Bananymous dabd79afa7 Userspace: Prepare aoc2023 environment :) 2023-12-01 01:22:53 +02:00
Bananymous 6ccb1bbbf9 init: set default termios on every username prompt
Before if e.g. Shell crashed init would have broken termios
2023-12-01 01:22:53 +02:00
Bananymous 9b841cb823 BuildSystem/Kernel: Enable -Wextra and -Werror in kernel
Only needed to fix some unused variable bugs
2023-12-01 01:22:53 +02:00
Bananymous c1cac43f28 BuildSystem: Don't build lai with -Wstack-usage
Lai has two functions that trigger warnings on gcc. There isn't
really anything I can do about it, so just disable the warning
2023-12-01 01:22:53 +02:00
Bananymous 8564b59e14 image: Remove inheritance from Netbpm
This inheritance made no sense
2023-11-29 20:56:05 +02:00
Bananymous fdb6dc94ba Kernel: cast between inheritance with static_cast
using reinterpret_cast is not a good idea. preferably we would use
dynamic_cast, but that is not possible since kernel is compiled with
-fno-rtti.
2023-11-29 20:50:57 +02:00
Bananymous 327b330338 Kernel: Make internal framebuffer bpp constexpr defined in libc 2023-11-29 20:07:33 +02:00
Bananymous 7090388c70 cp: allow copying all files except directories
The old condition seemed odd. This allows taking screen shots by
copying the /dev/fb0.
2023-11-29 16:13:40 +02:00
Bananymous ff550785a7 Userspace: Implement basic image rendering for Netbpm
You can now render Netbpm (P6) format image to framebuffer using
`image` command. I added basic test image to
/usr/share/images/sample.ppm
2023-11-29 16:11:35 +02:00
Bananymous d7a3aca5d4 Kernel: Use the correct bpp when writing to framebuffer 2023-11-29 00:31:24 +02:00
Bananymous 056586486d Shell: make clear use \e[2J instead of \e[J
This makes kernel to actually clear the full screen. If framebuffer
did not fit font exactly last row would be left partially uncleared
2023-11-28 23:55:37 +02:00
Bananymous 42a1d26d5b Userspace: Implement basic test for framebuffer mmap 2023-11-28 23:52:22 +02:00
Bananymous cc572af390 Kernel: Implement mmaping for framebuffer device 2023-11-28 23:51:56 +02:00
Bananymous 4275d2ce48 Kernel: Add framebuffer device to devfs 2023-11-28 23:51:28 +02:00
Bananymous 4a87d6052b Kernel: Add API for implementing mmappable devices 2023-11-28 23:50:49 +02:00
Bananymous d86ecf4f61 Kernel: Reading from negative offset in fb dev gives out info 2023-11-28 23:50:11 +02:00
Bananymous 09b7cb2f33 Kernel/LibC: Implement pread() 2023-11-28 23:47:30 +02:00
Bananymous 4c3da66c92 mmap-shared-test: Add test case for msync 2023-11-22 22:45:16 +02:00
Bananymous 60e755210c Kernel/LibC: Implement very basic msync 2023-11-22 22:44:06 +02:00
Bananymous ab9954fe73 Kernel: Delete the now obsolete VesaTerminalDriver 2023-11-22 21:59:11 +02:00
Bananymous fd18071975 Kernel: Implement TerminalDriver for Framebuffer device
Use this new FramebufferTerminalDriver for terminal instead of the
old VesaTerminalDriver. Only drawback with this is that framebuffer
device can only be intialized after DevFS is initialized.
2023-11-22 21:57:17 +02:00
Bananymous b88a7e0c6b Kernel: Add more APIs to FramebufferDevice 2023-11-22 21:56:27 +02:00
Bananymous cdf53f33f6 Kernel: Implement basic framebuffer device
This allows exposing framebuffer to userspace
2023-11-22 20:34:41 +02:00
Bananymous 25485069e6 Bootloader: Add cache to ext2 inode data block indices
This reduces the number of read calls with current kernel size from
~1700 to ~700 (60% performance boots). Loading the kernel is now alot
faster.
2023-11-22 13:54:53 +02:00
Bananymous f80bd040c8 Bootloader: add missing size directive 2023-11-21 19:11:48 +02:00
Bananymous bc5e8add19 Kernel: Make Ext2 filesystem use BlockDevice instead of Partition 2023-11-21 15:20:24 +02:00
Bananymous 7a8fd6d04a Kernel: TmpFS doesn't mark any functions as final
I didn't think these would be overloaded, but they are
2023-11-21 15:19:34 +02:00
Bananymous b749963b62 Kernel: Add common {read,write}_blocks() api to BlockDevice 2023-11-21 15:19:07 +02:00
Bananymous 6a068fb9f9 Kernel: Move Partition to its own file 2023-11-21 15:16:04 +02:00
Bananymous 19ed0cb9bf BAN: Add basic GUID data structure 2023-11-21 15:11:50 +02:00
Bananymous d08e876319 BuildSystem: Check value of BANAN_UEFI_BOOT with `if ((...)); then` 2023-11-21 11:53:50 +02:00
Bananymous f2a6f213dd BuildSystem: Add missing bootloader install script 2023-11-20 14:19:07 +02:00
Oskari Alaranta f7a5bfbccd Merge pull request 'BuildSystem: custom mount directory for bananos image to avoid conflicts' (#4) from Sinipelto/banan-os:main into main
Reviewed-on: Bananymous/banan-os#4
2023-11-20 14:16:58 +02:00
Sinipelto 6624821f55 BuildSystem: image sh
mount in build dir

Signed-off-by: Sinipelto <sinipelto@noreply.bananymous.com>
2023-11-20 14:12:35 +02:00
Sinipelto 328acd894f BuildSystem: image create sh
use banan build dir

Signed-off-by: Sinipelto <sinipelto@noreply.bananymous.com>
2023-11-20 14:11:29 +02:00
Sinipelto 01b17eaadc Update script/image.sh
mount dir default value

Signed-off-by: Sinipelto <sinipelto@noreply.bananymous.com>
2023-11-20 13:28:53 +02:00
Sinipelto fd16d6802c Update script/image-create.sh
custom mount dir

Signed-off-by: Sinipelto <sinipelto@noreply.bananymous.com>
2023-11-20 13:28:10 +02:00
Sinipelto fed2738805 Update script/image.sh
handle custom mount dir

Signed-off-by: Sinipelto <sinipelto@noreply.bananymous.com>
2023-11-20 13:24:15 +02:00
Sinipelto 9ad2ea8205 Merge pull request 'update main' (#1) from Bananymous/banan-os:main into main
Reviewed-on: #1
2023-11-20 13:20:51 +02:00
Bananymous f8f7e2208f BuildSystem: Remove old bootloader target
And creating image now builds the bootloader
2023-11-20 00:56:06 +02:00
Bananymous 8630f71f0c Toolchain: Build full toolchain with one call to toolchain/build.sh 2023-11-20 00:56:06 +02:00
Bananymous 9e44e8be75 Buildsystem: default bootloader is not my custom one
You can set BANAN_BOOTLOADER=GRUB to use grub instead. Image creation
does not convert disk image now automatically between bootloaders and
calling ./bos image-full is now required.
2023-11-18 17:18:03 +02:00
Bananymous cadb56d8ba Kernel: ProcFS inodes reflect processes ruid/rgid
setgid/setuid did not change the permissions of procfs inodes. This
made Shell launched by init not appear in meminfo.
2023-11-18 14:26:44 +02:00
Bananymous cd646a1ab7 Bootloader do some directory restructuring 2023-11-18 13:59:45 +02:00
Bananymous c9e9cfd361 Bootloader: Implement VESA video mode query and pass it to kernel
Kernel now gets framebuffer from bootloader. Framebuffer dimensions
and bpp are hardcoded in bootloader, but will probably be read from
config file at some point.
2023-11-17 22:45:35 +02:00
Bananymous ac96ea3370 Bootloader: Fix kernel memset to zero 2023-11-17 21:05:02 +02:00
Bananymous ef53aab24a Bootloader add temporary initial command line
This will probably be read from some config file at some point
2023-11-17 20:38:38 +02:00
Bananymous cb5a5d3ed1 Kernel/Bootloader: banan-os can now be booted with my bootloader :D 2023-11-17 20:33:02 +02:00
Bananymous d1444761a3 Bootloader: Clear screen, better memcpy
Clear screen before jumping to kernel. Memcpy now uses ebx as offset
register, so only one register has to updated every loop
2023-11-17 20:31:42 +02:00
Bananymous 95af728e39 Kernel: Don't calculate divisor in a for loop in ext2 inodes 2023-11-17 19:02:01 +02:00
Bananymous 24d87acec4 Kernel: Serial now uses random size for some serial ports
If the serial port doesn't repond with a size, just use a random
one. There is no reason to ditch the whole output if you cannot
determine its size.
2023-11-17 18:56:02 +02:00
Bananymous 84040e64b8 Kernel: Don't use multiboot2 explicitly. Parse it to common structure
This allows support of multiple different bootloaders
2023-11-17 18:54:59 +02:00
Bananymous 641a2dec00 Bootloader: Load kernel to memory and jump to it! 2023-11-17 16:36:29 +02:00
Bananymous 9e69053e64 Bootloader enter unreal mode at the start of stage2 2023-11-17 14:22:21 +02:00
Bananymous 1a415a380a Bootloader: Fix getting command line 2023-11-17 13:17:44 +02:00
Bananymous a19c5c672b Bootloader: implement reading from inode 2023-11-17 13:17:44 +02:00
Bananymous 3bcbc7c018 Bootloader: add support for indirect inode blocks 2023-11-16 13:34:21 +02:00
Bananymous b371abade5 Bootloader: Add helpers for printing n bit hexadecimal numbers 2023-11-16 13:30:01 +02:00
Bananymous 9d4101e0c5 Bootloader: Implement basic ext2 filesystem
This can search for files in an ext2 filesystem. Only 12 blocks
are currently supported.

Now only ELF loading is missing for loading the actual kernel!
2023-11-15 16:58:26 +02:00
Bananymous c791a1c200 Bootloader: Build with cmake instead of custom script 2023-11-14 03:44:47 +02:00
Bananymous bd3f2bb61c Bootloader: Split bootloader into multiple files
This cleans up the code since bootloader is starting to near 1k lines
2023-11-14 03:27:52 +02:00
Bananymous 7ca9a961b3 Bootloader move bootloader code from arch directory
The os itself only supports x86 so this won't matter. x86_64 and i386
use the same bootloader assembly.
2023-11-13 21:42:58 +02:00
Bananymous cbb9422ee0 Bootloader: installer now uses banan os elf headers intead of Linux's 2023-11-13 21:40:15 +02:00
Bananymous 2c2ee6636f fixup 2023-11-13 21:39:48 +02:00
Bananymous 3beebd721f LibELF: Remove 2 32 bit types that don't exist 2023-11-13 21:39:39 +02:00
Bananymous 048bbf874a Bootloader: Find root partition from GPT header 2023-11-13 18:55:48 +02:00
Bananymous d2970b5b8d Bootloader: installer now patches GPT GUID's 2023-11-13 18:53:55 +02:00
Bananymous 0004d3b435 Bootloader: Add API to create GUID from string 2023-11-13 18:52:41 +02:00
Bananymous d5aa08baa5 BuildSystem: add bootloader target
Use this target to run banan-os with custom bootloader
2023-11-12 01:50:30 +02:00
Bananymous 5362962d9a BuildSystem: add proper clean target 2023-11-12 01:14:42 +02:00
Bananymous bfe6d60e9e ls: print link targets when listing files 2023-11-11 23:17:18 +02:00
Bananymous c084ce8b01 Kernel/LibC: Implement readlink and readlinkat 2023-11-11 23:16:52 +02:00
Bananymous 381cfdad77 Bootloader: Continue work on bootloader
Bootloader can now get the memory map and read cmdline from user.

Now 'just' video mode query, ext2 and ELF parsing are needed :D
2023-11-11 22:49:00 +02:00
Bananymous cfc7313451 Bootloader: Start work on bootloader
I wrote a fast first stage bootloader and a installer to put it into
a disk image.
2023-11-09 22:42:47 +02:00
Bananymous bc8fd1285f Toolchain: Fix typo when setting make flags
I defaultet MAKEFLAGS to -j which will launch processes in parallel
without any limit.
2023-11-09 21:57:45 +02:00
Bananymous 8e5224ef53 Toolchain: add em=gnu to gas. This allows using / in expressions 2023-11-09 21:43:13 +02:00
Bananymous fda0dfec30 Kernel: Make TmpFS enforce max page count. 2023-11-07 16:13:21 +02:00
Bananymous d6ae1bcf36 Kernel: Remove now obsolete RamFS
Everything is using now the better TmpFS which uses physical pages
for page allocation instead of the static kmalloc memory.
2023-11-07 16:07:11 +02:00
Bananymous 5044810451 Kernel: Make DevFS use the new and better TmpFS instead of RamFS 2023-11-07 16:05:05 +02:00
Bananymous 147cd93ed3 Kernel: Add method to TmpFS for looping over all (cached) inodes 2023-11-07 16:04:34 +02:00
Bananymous 07b5920f3f Kernel: Lock TmpFS in all its methods 2023-11-07 16:03:52 +02:00
Bananymous 2bcf934389 Kernel: Implement symlinks to TmpFS 2023-11-07 15:59:50 +02:00
Bananymous 1405712f26 Kernel: Make PS/2 keyboard wait until interrupts are enabled 2023-11-07 15:58:50 +02:00
Bananymous a8ffe6b3a6 BuildSystem: Fix temporary sysroot creation in toolchain compilation 2023-11-07 14:16:49 +02:00
Bananymous 613d6640fe meminfo: better format for files without permissions 2023-11-07 02:41:01 +02:00
Bananymous 885ed218fa Kernel: Make unlinking from /proc always fail with EPERM 2023-11-07 02:40:27 +02:00
Bananymous b1f431d962 Kernel: /tmp is now TmpFS instead of RamFS 2023-11-07 02:36:22 +02:00
Bananymous 06e176e6b9 Kernel: Make ProcFS use the new TmpFS internally 2023-11-07 02:35:44 +02:00
Bananymous b7771e95ac Kernel: Implement TmpFS Inode unlinking and deletion 2023-11-06 21:49:12 +02:00
Bananymous 3e33fc156b Kernel: TmpFS directory inodes now iterate over only valid entries 2023-11-06 21:41:51 +02:00
Bananymous 438f01a856 Kernel: Fix TmpFS directory entry enumeration early return 2023-11-06 21:06:10 +02:00
Bananymous 5dd8189048 Kernel: Fix ext2 directory listing for big directories 2023-11-06 21:05:58 +02:00
Bananymous ff6b127c3a Kernel: Implement TmpFS directory listing 2023-11-06 21:05:58 +02:00
Bananymous c3ed700324 Kernel: Implement TmpFS inode chmod 2023-11-06 20:11:34 +02:00
Bananymous ba2f09a4e6 Kernel: Cleanup TmpFS code and block access doesn't require allocs
TmpFS blocks are now accessed with a simple wrapper
2023-11-06 20:07:09 +02:00
Bananymous c92c1b8e2b Kernel: TmpInode blocks are on demand allocated 2023-11-06 10:44:37 +02:00
Bananymous c27d20abd8 Kernel: Implement read/write/truncate for TmpFileInode 2023-11-05 02:28:43 +02:00
Bananymous 99d7b0917d Kernel: Start work on proper TmpFS in Heap instead of kmalloc memory 2023-11-04 18:18:45 +02:00
Bananymous 8b4e129fc1 Kernel: Add better APIs for fast page 2023-11-04 18:13:16 +02:00
Bananymous 240d57f4b4 BAN: Update bytespan -> span API 2023-11-04 18:12:46 +02:00
Bananymous f6c312a6b3 BuildSystem: using sysroot doesn't need root privileges anymore!
Sysroot is now created with fakeroot. This allows root access to be
only needed for disk image creation, since it uses loopback devices.
2023-11-04 17:50:43 +02:00
Bananymous ca34ece8ef BAN: Update ASSERT api
its now much harder to mix < with <= and > with >=
2023-11-02 00:01:12 +02:00
Bananymous 71ecdd7245 Kernel: Canonicalize vaddr before using it 2023-10-30 19:20:17 +02:00
Bananymous b80b59ce24 Kernel: Remove unused externs in kernel.cpp 2023-10-30 19:09:31 +02:00
Bananymous 640d27748f Kernel: Temporarily force FileBackedRegion mappings writable
Now that write-protect bit is enabled this is neccessary.
2023-10-30 19:08:33 +02:00
Bananymous 42772611ce Kernel: Add fast page to page table
Add "fast page" to KERNEL_OFFSET. This is always present in page
tables and only requires changing the page table entry to map. This
requires no interrupts since it should only be for very operations
like memcpy.

I used to map all temporary mappings to vaddr 0, but this is much
better. C++ standard always says that nullptr access is undefined
and this gets rid of it.

Fixed some bugs I found along the way
2023-10-30 19:02:09 +02:00
Bananymous 6e676ee8c5 Kernel: Remove GeneralAllocator since it was not used 2023-10-30 18:13:17 +02:00
Bananymous 066fb44105 Kernel: Allow offsetof with packed fields
This is not standard C++ but should be fine with my toolchain.
2023-10-30 17:51:18 +02:00
Bananymous 3f638f125e Kernel: panic takes arguments as rvalue references 2023-10-30 16:22:02 +02:00
Bananymous 35f8f44510 Kernel: Fix ext2 small link deallocation
Also fix deallocation bug
2023-10-30 16:22:02 +02:00
Bananymous 0c590821ed Kernel: only map kernel from g_kernel_start onwards 2023-10-30 16:22:02 +02:00
Bananymous 3c311efb81 BAN: Implement better ASSERT macros
Implement macros for all basic binary ops. These macros print failed
values when the fail.
2023-10-30 16:22:02 +02:00
Bananymous 6ef153b6ca Kernel: Enable Write Protect. This seems to be good for security 2023-10-30 12:23:22 +02:00
Bananymous 130a69bac6 Kernel: Fix stack OOB detection
I now check both interrupt and normal stack to detect OOB. Processes
are killed if they encouner stack over/under flow.
2023-10-30 12:17:08 +02:00
Bananymous b5d873dfae Kernel/LibELF: Map pages always as RW so kernel can write to them
Kernel doesn't seem to require W permissions to a page without UEFI
so this had not been found earlier.
2023-10-30 12:14:12 +02:00
Bananymous d5e0900cbb BAN: String now uses union for its sso storage
This allows String to shrink by 8 bytes since Variant's 8 index is
no longer stored in here.

This required me to make Strings max size one bit less, but that
should still be fine. There should never be strings with size of
over half of the computer's address space.
2023-10-30 11:13:16 +02:00
Bananymous a63006afaf Userspace: Add quick test for global ctors and dtors 2023-10-30 11:11:10 +02:00
Bananymous 7c6832cee4 LibC: implement and call __cxa_finalize() on exit()
This allows global destructors to be actually called
2023-10-30 11:10:08 +02:00
Bananymous 0b5fcb3f88 Kernel/LibC: Add crt* files to LibC and remove crt0 from kernel
There was no reason for libc get crt0 from kernel.
2023-10-30 11:06:13 +02:00
Bananymous f33c0bad99 Toolchain: Clone GCC and Binutils from git
This feels much cleaner than just downloading tar balls from
pregiven urls. Also patching works much better like this!

I added --disable-initfini-array since global constructors were
not called.
2023-10-30 11:02:57 +02:00
Bananymous 18f61c5427 Kernel: Fix ACPI DSDT address
Read x_dsdt address only if fadt's length contains it. Bochs seems
to have version 1 fadt without the x_* fields.
2023-10-29 21:35:11 +02:00
Bananymous 229082a1b2 BAN: Rewrite String with small string optimizations
String now holds a 15 byte sso buffer. I'm not sure what the size
should actually be but 15 will work for now. Maybe the sso buffer
should be contained in an union with one bit flag in size instead of
variant that uses extra 8 bytes for type index.

This patch buffs sizeof(String) from 24 bytes to 32 bytes on 64 bit.
I assume this is much better version than the old which had to make
allocation even for empty strings :D.
2023-10-29 03:11:13 +03:00
Bananymous 0db17e9d39 BAN: Add variant to ForwardList
I should be using the forward list more
2023-10-29 02:28:55 +03:00
Bananymous 2d34b2b8a3 BuildSystem: Add bos short hand for building with zsh completions :) 2023-10-28 22:23:29 +03:00
Bananymous 76f48f095c Kernel: Rewrite a lot of ext2 code
This commit consists of multiple big changes

1. blocks for inodes are now allocated on demand
  - reading from non allocated block will just return zeroes
  - writing to non allocated block allocates it

2. code doesn't really use raw pointers anymore
  - all casts to uint32_t or structures are now replaced with
    spans. either as<T> or as_span<T> which both are bounds
	checked

3. code doesn't depend on random macros for accessing indirect blocks
  - i added some recursive functions which take care of this :)
2023-10-28 22:13:28 +03:00
Bananymous d98f84f9d3 BAN: Update ByteSpan API
Add ByteSpan::as_span<T> and const versions of as() and as_span()
require T to be const.
2023-10-28 22:10:47 +03:00
Bananymous 240a687d8f Kernel: Fix ext2 inode block allocation with triply indirect blocks 2023-10-28 16:53:00 +03:00
Bananymous 35b46e37bb Kernel: Fix ext2 inode deletion cleanup
I now cleanup all blocks (including direct) in i_block array
2023-10-28 16:52:56 +03:00
Bananymous 14d24bce98 All: Remove read only from ext2 filesystem :) 2023-10-26 13:26:10 +03:00
Bananymous ea0d7156a4 rm: add option to remove recursively 2023-10-26 02:32:49 +03:00
Bananymous 38c267b4c8 Kernel: Fix ext2 inode deletion
fsck now reports clean filesystem even after deleting files
2023-10-26 02:05:05 +03:00
Bananymous 81228d3401 Userspace: implement basic rm command 2023-10-25 21:45:27 +03:00
Bananymous 87ff38664a Kernel/LibC: Add syscall and wrapper for unlink 2023-10-25 21:45:04 +03:00
Bananymous b7007016c0 BAN: Implement Ext2 file unlinking
Ext2 inodes can now be unlinked from directories and after last
inode closes (destructor gets called) we check if link count is 0
and cleanup the inode from filesystem
2023-10-25 21:43:36 +03:00
Bananymous 3566ddab00 BAN: ByteSpan can be sliced without specified size
This will give span with all remaining size after offset
2023-10-25 21:41:11 +03:00
Bananymous 6ee4d10651 Kernel/LibC/Userspace: Implement mkdir and creat
Touch now uses creat insteadd of open with O_CREAT flag
2023-10-25 21:07:26 +03:00
Bananymous e9b7cf332d Kernel: Implement directory creation for RamFS 2023-10-25 19:41:34 +03:00
Bananymous 4ee2f0f789 Kernel: Implement Ext2 directory creation 2023-10-25 19:37:04 +03:00
Bananymous 18e90d305d Kernel: Add Inode API for creating directories 2023-10-25 19:36:04 +03:00
Bananymous fd1b331b86 Kernel: Ext2FS now uses Ext2Inodes as cached values 2023-10-25 19:34:00 +03:00
Bananymous 5c51d09309 Kernel: Fix read offset of RamFileInode 2023-10-25 02:53:20 +03:00
Bananymous c445ea6266 cp: abort copy if write fails 2023-10-25 02:43:02 +03:00
Bananymous e685f38fd1 Userspace: Add basic chmod command 2023-10-25 02:37:19 +03:00
Bananymous 627b8cc140 Kernel/LibC: implement chmod syscall + libc wrapper 2023-10-25 02:35:37 +03:00
Bananymous 657f95d3f0 Userspace: Implement basic cp
This does not support any meaningful command line arguments but
is a good start.
2023-10-25 00:07:25 +03:00
Bananymous 07f8c972b3 Shell: Set get old termios earlier
I sourced the config file before getting old termios. Sourcing
updated the termios so old_termios was always in non canonical, non
echoing mode.
2023-10-24 19:10:53 +03:00
Bananymous fc8a000e46 BAN: Fix bug of size of splice after slice()
I have no idea what was I doing before :D
2023-10-24 17:23:45 +03:00
Bananymous 6f6ccbfa04 meminfo: fix g++ warning for oob write
g++ doesn't realize that read can only return -1
2023-10-24 16:50:21 +03:00
Bananymous c71ac588b2 BuildSystem: Fix bugs in new build system
I had not tested the new build system with clean toolchain build
but it seems to work now.
2023-10-24 16:48:46 +03:00
Bananymous 2d0da93ac4 Kernel: Add timeout to ACHI commands
ACHI commands can now fail from timeouts.
2023-10-24 11:56:25 +03:00
Bananymous fb801044ec BuildSystem: Match README.md with the new buildsystem 2023-10-24 11:56:00 +03:00
Bananymous ce87e0a605 BuildSystem: Rewrite whole build system structure
Now you have to use script/build.sh for building and running banan-os
2023-10-24 11:56:00 +03:00
Bananymous f3d9da9549 Kernel: Rewrite all read/write functions to use BAN::ByteSpan
This allows us to not work with raw pointers and use sized containers
for reading and writing.
2023-10-24 11:56:00 +03:00
Bananymous 71cad2fc38 BAN: Implement ByteSpan
This is a span over exisiting containers/data types. I'm not too
happy with the constructors and assignment operators, but they will
work for now
2023-10-20 04:59:29 +03:00
Bananymous 61c7a68f4a BAN: add helper to cast Span<T> to Span<const T> 2023-10-20 04:59:08 +03:00
Bananymous c17cdb39d5 Kernel: Map multiboot2 memory in PageTable initialization
It cannot be assumed that multiboot data lies between kernel_end
and 2 GiB mark, so I properly allocate virtual address space for it.
2023-10-17 01:15:08 +03:00
Bananymous 69a39b7077 Kernel: Start using multiboot2 instead of multiboot
This allows better compatibility with (U)EFI and gives RSDP location
instead of me having to scan ram to find it.
2023-10-17 01:06:24 +03:00
Bananymous 19696bdad3 Kernel: TTY doesn't panic if it doesn't find input device 2023-10-16 16:58:39 +03:00
Bananymous 00dd7d85ce Kernel: Fix device identification with all bits as ones
If device identification sends all ones, don't initialize the device.
2023-10-16 16:57:07 +03:00
Bananymous 0c88c74b76 Kernel: ATABuses are but to compatibility mode if possible
I don't support native mode ata bus (irq sharing) so ata buses are
but to compatibility mode if possible.
2023-10-16 16:56:12 +03:00
Bananymous c4a640bcb6 Kernel: Fix ATADevice naming
ATADevice now stores its name instead of using static buffer. Old
static buffer was changing on every name query. I just hadn't noticed
since virtual machine disks were always sda.
2023-10-16 16:52:15 +03:00
Bananymous 0fdf8b6f68 Kernel: Fix PCI bugs
IO BarRegion used vaddr instead of the correct paddr. Add API for
memory region iobase query.
2023-10-16 16:50:49 +03:00
Bananymous 37bc52988c Kernel: Don't require framebuffer
Initializes virtual tty only if framebuffer is initialized
2023-10-16 01:44:54 +03:00
Bananymous 1312a9dad2 Kernel: PCI checks if ethernet device is E1000 before initialization
I used to treat all ethernet deivices as E1000 but now it is actually
verified before initialization
2023-10-16 01:44:54 +03:00
Bananymous 73b9c28457 Kernel: PCIDevice stores its vendor id and device id 2023-10-16 01:44:54 +03:00
Bananymous 29db8d0d40 BuildSystem: add cmake variable UEFI_BOOT
If this variable is defined in cmake, image will be build with esp
and booted with uefi.
2023-10-16 01:44:54 +03:00
Bananymous 5bf0f9742a Toolchain: Build grub with efi capabilities 2023-10-16 01:37:12 +03:00
Bananymous fc5bfa2cb3 Kernel: Clear TTY when setting as current
Actually this should replace from old buffer, but this works
for now.
2023-10-13 17:20:26 +03:00
Bananymous f7e38ccfe4 Kernel: Don't crash if header type != 0 in bar region creation
Also remove spammy debug printing
2023-10-13 16:32:32 +03:00
Bananymous 128202a55a Kernel: Cleanup GDT code 2023-10-13 16:18:22 +03:00
Bananymous 89d74a9088 Kernel: Separate scheduler execution and stack loading
Not sure if this is actually needed, but this allows actual
executing function to be in clean environment
2023-10-13 16:17:27 +03:00
Bananymous 63f64619bc Kernel: add NEVER_INLINE and make Interruptable not constructable 2023-10-13 16:17:27 +03:00
Bananymous 518fd3fad0 Kernel: Add 16 more irq handlers
IDT will now panic if trying to assing handler for non supported
irq.
2023-10-13 16:17:27 +03:00
Bananymous 74b77ba2ac Kernel: Debug temporary debug print just to beginning when full 2023-10-13 12:43:52 +03:00
Bananymous 888e44ce0b Kernel: Allow getting ACPI headers with same signature 2023-10-13 11:24:21 +03:00
Bananymous b3b8376cca Kernel: Fix PhysicalRange mapping size 2023-10-13 03:45:01 +03:00
Bananymous 9c143d18b9 Kernel: Add temporary terminal output before controlling terminal
Starting work on getting this boot on real hardware.
2023-10-13 03:31:36 +03:00
Bananymous 9a6cc0dc2d Kernel: Check whether ELF address space can be loaded
Before reserving address space in SYS_EXEC verify that ELF address
space is actually loadable. For example when trying to execute the
kernel binary in userspace, binarys address space would overlap with
current kernel address space. Now kernel won't crash anymore and
will just send SIGKILL to the process calling exec*().
2023-10-12 22:59:36 +03:00
Bananymous 8c792f9c6d Shell: Fix parsing $ with unknown character 2023-10-12 22:24:27 +03:00
Bananymous adc5ff9e99 Kernel: Implement AHCI driver
SATA drives can now be used with banan-os. This allows much faster
disk access (writing 10 MiB from 30s to 1.5s). This can definitely
be optimized but the main slow down is probably the whole disk
structure in the os.

AHCI drive is now the default when running qemu.
2023-10-12 21:53:48 +03:00
Bananymous bde9ff9319 Kernel: Generalize ATA device and cleanup code 2023-10-12 21:35:25 +03:00
Bananymous 75fdf4c3c6 Kernel: Add Timer::ns_since_boot() 2023-10-12 21:16:39 +03:00
Bananymous b723ed5fd2 Kernel: Add vaddr/paddr conversion functions to DMARegion 2023-10-12 15:20:05 +03:00
Bananymous 89c975350d Kernel: PCI can now get interrupts for devices 2023-10-11 22:18:58 +03:00
Bananymous f27974dd3c Kernel: PCI cleanup PCI::Device API 2023-10-11 21:52:08 +03:00
Bananymous 744ff40ba0 BAN: Optional can now be constructed from another Optional
Also fix bug in release_value() where we did not call the
destructor.
2023-10-11 20:10:06 +03:00
Bananymous d4c9f371a6 Kernel: make DMARegion::create static and fix mapping 2023-10-08 18:18:36 +03:00
Bananymous 2aabf43ace Kernel: fix some math in physical ranges
I allocated 1 bitmap page per 8 data pages. Bitmap page can actually
store 8*PAGE_SIZE data pages.

Also properly set last bits in bitmap. I did not care about endianness
but now we set the bits on unsigned long longs instead of bytes.
2023-10-08 13:25:34 +03:00
Bananymous a6ca9fd453 Kernel: Implement bare boness DMA Region
This does nothing but allocate contiguous physical and virtual memory
and map it as CacheDisable. Also memory is automatically freed RAII style.
2023-10-08 02:57:22 +03:00
Bananymous 799aab02f5 Kernel: Add API for getting contiguous physcial pages
This will be used to create DMA regions.
2023-10-08 02:57:22 +03:00
Bananymous 0fae2c7309 Kernel: Rework physical memory allocation
PhysicalRange is now much simpler bitmap. This makes expanding
PhysicalRange API much easier.
2023-10-08 02:50:23 +03:00
Bananymous 7ba72b1507 Kernel: Fix PCI BarRegion offsets
Calculations accidentally assumed bar registers are 8 byte instead
of 4.
2023-10-08 02:50:23 +03:00
Bananymous f21d4e794c Kernel: Rework whole ATA driver structure
Make ATA driver more compatible when we are adding SATA support
2023-10-08 02:50:23 +03:00
Bananymous 27eb5af6f0 Kernel: Rework interrupt mechanism
All interruptrable classes now inherit from Interruptable which
has methdo handle_irq which is called on a interrupt.
2023-10-05 18:53:45 +03:00
Bananymous 68a913c838 BuildSystem: Don't strip kernel 2023-10-05 18:52:44 +03:00
Bananymous a084db6817 BuildSystem: Try to set compiler only if it exists 2023-10-05 18:52:05 +03:00
Bananymous bc4d272c4f cat: Use write() instead of puts to print file contents
This allows printing files that contain null bytes behave more like
you would expect
2023-10-04 22:16:19 +03:00
Bananymous 57605c2b3e meminfo: Print allocated physical memory percentage 2023-10-03 10:39:27 +03:00
Bananymous 1cd99631e1 Shell: source $HOME/.shellrc if found on Shell startup 2023-10-03 10:39:27 +03:00
Bananymous f67ed9a66e Shell: Implement sourcing scripts 2023-10-03 10:24:10 +03:00
Bananymous 6c11dac113 meminfo: Add process command line to the output 2023-09-30 23:17:31 +03:00
Bananymous 39bc6c43dc Kernel: Expose command line and environment to /proc 2023-09-30 23:01:33 +03:00
Bananymous 5ee3506474 Kernel: Add physical memory info to /proc/{pid}/meminfo 2023-09-30 22:11:45 +03:00
Bananymous c5119cda97 Userspace: Add meminfo command that parses /proc/{pid}/meminfo 2023-09-30 21:20:53 +03:00
Bananymous 785de5f9b9 Kernel: /proc/{pid}/meminfo now reports per process memory usage 2023-09-30 21:20:18 +03:00
Bananymous 8f630a97df Kernel: Add procfs that contains only pids 2023-09-30 21:19:36 +03:00
Bananymous 56bb419884 Kernel: All process' memory areas can report their virtual mem usage 2023-09-30 21:15:46 +03:00
Bananymous d7a00e8cc2 LibC: Implement stpcpy since gcc seems to need it
gcc seems to optimize some calls to strcpy to stpcpy
2023-09-30 20:58:19 +03:00
Bananymous 9f0797047f Kernel/LibC: dirent now contains file type 2023-09-30 20:46:57 +03:00
Bananymous e8a0eeb0b4 Kernel: RamInode verifies that you have not specified mode type
This is kinda weird behaviour, but it ensures the user cannot
create e.g. CharacterDevice with mode having IFLNK.

The Inode overrider is the only one setting the mode.
2023-09-30 20:46:57 +03:00
Bananymous 3e5645d453 Kernel: Add API for RamDirectoryInodes to delete containing inodes 2023-09-30 19:22:30 +03:00
Bananymous 2301654c4e Kernel: Rework RamInode API
RamInode is now a general RamInode with no data. RamFileInode is now
a inode for regular files. This is much cleaner and more intuitive
since there is no reason for most non-regular inodes to hold data
Vector.
2023-09-30 19:22:30 +03:00
Bananymous 0b93fce923 BAN: Remove endianness functions from Math
There is now a Endianness.h for these. The functions were super slow.
2023-09-29 19:38:07 +03:00
Bananymous 6304388100 Shell: Quick fix to not freeze for multiple seconds
When sync is writing to disk, it reserves whole disk to itself.
This commit makes Shell to read username only once from getpwuid().
We used to get username every time prompt was printed.
2023-09-29 19:20:48 +03:00
Bananymous 96b1186c19 Userspace: Implement basic test for MAP_SHARED 2023-09-29 18:59:37 +03:00
Bananymous 43c23db4a6 Kernel: Implement MAP_SHARED for regular files
Every inode holds a weak pointer to shared file data. This contains
physical addresses of pages for inode file data. Physical addresses
are allocated and read on demand.

When last shared mapping is unmapped. The inodes shared data is freed
and written to the inode.
2023-09-29 18:59:20 +03:00
Bananymous 8ff4e1f8c8 Kernel: Don't write to stat_loc on SYS_WAIT if it is null 2023-09-29 18:31:44 +03:00
Bananymous b57811b371 Userspace: Implement cat-mmap
This behaves exactly as cat, but uses mmap to read the file.
2023-09-29 17:24:55 +03:00
Bananymous 23f429e23b LibC: Fix mmap()
mmap() did not pass fildes to the syscall structure.
2023-09-29 17:24:21 +03:00
Bananymous 7b1c573ad0 Kernel: Implement MAP_PRIVATE file mappings
mmap() now supports mapping files with MAP_PRIVATE.
2023-09-29 17:23:42 +03:00
Bananymous 4ee759aa3b Kernel: Implement new abstract MemoryRegion
MemoryBackedRegion now inherits from this and is used for private
anonymous mappigs. This will make shared mappings and file backed
mappings much easier to implement.
2023-09-29 16:18:23 +03:00
Bananymous 48096b18c2 LibC: mmap returns MAP_FAILED instead of NULL 2023-09-29 10:38:08 +03:00
Bananymous 06af9f3187 LibELF: Optimize LoadableELF::clone() memory usage
We only clone mapped pages that have been marked as writeble.
Read/execute only pages will be exactly as in the file itself and
can be demand paged also :D
2023-09-29 02:05:12 +03:00
Bananymous 16eb055737 Kernel: Add some sanity assertions/functions 2023-09-29 02:03:19 +03:00
Bananymous a34f2e6e0d Kernel: Use the new on demand ELF structure
All executable files are now read from disk and paged on demand.
This was a big rewrite of the old ELF library but in the end
everything seems much cleaner, since all the old functionality was
not actually needed for execution.

I have to do some measurements, but I feel like memory usage dropped
quite a bit after this change.
2023-09-29 02:00:10 +03:00
Bananymous 8357dd7b3d LibELF: Implement new ELF structure
This structure is used for demand pagable execution. It handles all
memory allocation and file reading.
2023-09-29 01:58:03 +03:00
Bananymous 5bdf7d313f LibELF: Add types for native executable 2023-09-29 01:56:57 +03:00
Bananymous 5b6569f2c9 Kernel: Fix off by one error when calculating pages in range 2023-09-29 01:56:15 +03:00
Bananymous 58f4ed22c4 Kernel: Don't map interrupt stack as userspace accessable 2023-09-28 21:58:24 +03:00
Bananymous 245f58cc3a Kernel: mmap regions are now demand paged
mmap will not actually take any memory unless you use the given
memory.
2023-09-28 21:07:14 +03:00
Bananymous 15cd59b8ce LibC: Fix a bug in malloc
You could not allocate with size equal to one of the pool sizes.
2023-09-28 21:05:27 +03:00
Bananymous d9a5e471f5 Kernel: Remove duplicate code in VirtualRange::create_to_vaddr_range 2023-09-28 13:59:49 +03:00
Bananymous d7ecf94347 Kernel: Reboot will now always succeed
If acpi reset fails, we forcefully trigger a triple fault to restart
the system.
2023-09-28 13:53:03 +03:00
Bananymous 6eda65eea6 Kernel/LibC/Userspace: Add SYS_POWEROFF + cli tool
You can now shutdown/reboot banan-os with the poweroff cli tool.

Reboot doesn't seem to work on qemu.
2023-09-28 12:36:47 +03:00
Bananymous d45bf363f1 Kernel: Enter ACPI mode with lai 2023-09-28 12:30:27 +03:00
Bananymous 1abf787596 LibC: Add errno for unknown error 2023-09-28 12:06:17 +03:00
Bananymous 3b283cb860 Kernel: Make tty overload correct has_data() function
This allows snake game to work again :)
2023-09-28 11:54:12 +03:00
Bananymous 4a01e4c3b4 Userspace: Use printf length modifiers when printing 2023-09-28 11:49:31 +03:00
Bananymous 61694268e2 LibC: Implement length modifiers to printf 2023-09-28 11:42:57 +03:00
Bananymous 399d5338c6 Shell: String leading and trailing whitespace from commands
This fixes a bug of inserting empty argument if command had trailing
whitespace
2023-09-28 10:28:49 +03:00
Bananymous a8d74f604e BAN: Update Endiannes API
Add functions to swap endiannes or convert host to big/little endian

This code should be very compiler friendly and should be optimized to
single bswap instruction on x86.
2023-09-28 01:22:14 +03:00
Bananymous cb76f1ea75 Kernel: Add some functionality to disable TTY input/output
Userspace programs can call tty_ctrl() to disable/enable tty from
handling input and displaying output.

This API is probably going to change in the future to ioctl calls
but I'm not sure how ioctl is used and what functionality should it
have. I decided to create whole new function and syscall for now.

Next I will expose framebuffer in /dev/fb0 and then I can start work
on graphical environment! :D
2023-09-27 15:44:05 +03:00
Bananymous 459afef89e Kernel: Disable DiskCache sync messages 2023-09-27 14:12:21 +03:00
Bananymous 8621d8a155 BuildSystem: Rework calling qemu
I change always manually the serial/graphical. When running cmake
you can define variable QEMU_ACCEL that will be used as accelerator.

Also ninja has the following targets for running qemu
  1. qemu:            Run graphical qemu environment
  2. qemu-nographic:  Run qemu without graphical screen. You should
                      select 'serial only' from grub menu.
  3. qemu-debug:      Run qemu without accelerator and interrupt
                      debugger.
2023-09-27 13:49:01 +03:00
Bananymous 1c26a402b0 Kernel: Add missing TRY() to font loading 2023-09-27 00:49:53 +03:00
Bananymous b573701625 Sync: Add some argument parsing to sync(1)
You can specify --block to make the program wait until sync is
complete.
2023-09-27 00:37:23 +03:00
Bananymous a69e5fb288 LibC: add syncsync() to unistd.h
This is my own WELL NAMED (:D) function that takes a paramemeter
to make the sync operation synchronous.
2023-09-27 00:35:36 +03:00
Bananymous 1e6930a3bc Kernel: SYS_SYNC now schedules sync to happen soon
You can pass non-zero argument to the syscall to block until the
sync has finished.
2023-09-27 00:34:00 +03:00
Bananymous 11db49e2d3 Kernel: DiskCache now requires sync to be called from kernel thread
This disables the scenario where user interrupts sync operation
possibly leaving the syncing in invalid state.
2023-09-27 00:32:13 +03:00
Bananymous c33e658f98 Kernel: Allow creationg of empty processes and manual registration
You can now create kernel processes without any threads, add the
needed threads and only then register the process and its threads
to the scheduler.
2023-09-27 00:29:45 +03:00
Bananymous 22252cfcf0 Kernel: All syscalls now validate users pointers
We now validate pointers passed by the user, to forbid arbitary
memory read/write. Now the user is only allowed to pass in pointers
in their own mapped memory space (or null).
2023-09-25 22:07:12 +03:00
Bananymous 976114fde1 Kernel: Print more detailed output on ISR 2023-09-25 20:33:07 +03:00
Bananymous 9d55cf1d80 Kernel: Ext2 inodes are now stored in cache
This allows faster inode access and ensures working inodes when
opened in multiple places.
2023-09-25 20:31:40 +03:00
Bananymous ef68e12125 BAN: Implement basic WeakPtr
This can be constructed from classes that inherit from Weakable
2023-09-25 19:43:10 +03:00
Bananymous 669b2ace4e Kernel: Ext2 filesystem now holds 10 preallocated block buffers
Inodes can query blocks from this buffer. This allows allocation of
blocks to not fail during normal operations. Also less stress on
kmalloc.
2023-09-25 14:32:25 +03:00
Bananymous 0e67c6318b Kernel: Fix ext2 block allocation
Redo ext2 block allocation. This is now much "cleaner" although
I'm not too fond of the macros.
2023-09-25 13:17:44 +03:00
Bananymous 959fb64752 BuildSystem: Optimize image creation
We now use truncate to create disk image, since it doesn't require
writing zeroes to full disk. I also removed creation of third
partition as this was not currently used.
2023-09-25 13:15:55 +03:00
Bananymous ffa80d0466 Kernel: Hacky kmalloc quick fix
Remove GeneralAllocator from kmalloc as it is not CriticalScope safe.
This requires increasing kmalloc memory.
2023-09-25 13:13:57 +03:00
Bananymous e7ca83ecb2 Kernel: VirtualRange doesn't store physical addresses of pages
This was unnecessarry allocation, since the page table allready
contains virtual address -> physical address mappings.
2023-09-24 01:29:34 +03:00
Bananymous f6261e5dc9 Kernel: all mapped ranges are now stored in one container
We just now have a flag if a mapping is unmappable
2023-09-23 23:45:26 +03:00
Bananymous d1bbbf48f6 Kernel: Fully remove sys_alloc and sys_free
I could delete the whole FixedWidthAllocator as it was now obsolete.
GeneralAllocator is still used by kmalloc. Kmalloc cannot actually
use it since, GeneralAllocator depends on SpinLock and kmalloc runs
without interrupts.
2023-09-23 03:53:30 +03:00
Bananymous 3ba15b41a3 Kernel/LibC: remove PATH resoltion from kernel
I have no idea why I had made PATH environment variable parsing
to be part of the kernel. Now the shell does the parsing and
environment syscall is no longer needed.
2023-09-23 03:08:14 +03:00
Bananymous 7a7c5e433e Kernel/LibC: add flag to enable/disable sse support
SSE support is very experimental and causes GP. I decided to make
SSE not default until I get to fixing it :)
2023-09-23 02:28:25 +03:00
Bananymous 6bb2c80bdd Kernel/LibC: userspace malloc now uses mmap to get memory
We could remove syscalls to allocate more memory. This was not
something the kernel should have done.
2023-09-23 02:26:23 +03:00
Bananymous af4af1cae9 Kernel/LibC: add mmap for private anonymous mappings
This will be used by the userspace to get more memory. Currently
kernel handles all allocations, which is not preferable.
2023-09-22 23:01:14 +03:00
Bananymous b9c779ff7e BuildSystem: Add cmake target for debugging qemu 2023-09-22 17:20:35 +03:00
Bananymous 2a469241b2 BAN: Fix LinkedList::pop_back() 2023-09-22 17:20:35 +03:00
Bananymous 83b165ebb0 Kernel: Add constexpr conditional debug prints 2023-09-22 17:20:35 +03:00
Bananymous c5f9f0c307 Kernel: Add LAI as a dependency
I did not feel like implementing AML interpreter now, and wanted
everything AML has to offer. I will be writing my own AML interperter
at some point.
2023-09-22 17:20:35 +03:00
Bananymous 9d827037ca Kernel: Fix timer includes 2023-09-22 17:20:35 +03:00
Bananymous 58506c5bd1 Kernel: Add config read/write api to PCI 2023-09-22 17:20:35 +03:00
Bananymous dd0b8c4140 Kernel: Add pointer validation API to page table 2023-09-22 17:20:35 +03:00
Bananymous d1183f0bf6 General: remove linecount.sh 2023-09-22 17:20:35 +03:00
Bananymous 11717f90c1 Kernel: PCI devices can now create region for BAR
This creates either MEM or IO region for read/write access to PCI
device.
2023-09-22 17:20:35 +03:00
Bananymous a740bf8df4 1000th COMMIT: Kernel: Add basic E1000 driver
This driver is only capable to read mac address and enable and read
link status
2023-09-22 17:20:28 +03:00
Bananymous 63dc2b6aa6 Kernel: Implement SYS_SYNC and add sync executable to userspace
You can (and have to) manually sync disk after writes to it.
2023-09-11 01:26:27 +03:00
Bananymous eee0537053 Kernel: Writes to disk are not synchronous anymore
Implement "proper" DiskCache syncing
2023-09-11 01:25:16 +03:00
Bananymous 7ec860a3d4 Kernel: Print stack trace on isr 2023-09-11 01:20:55 +03:00
Bananymous 9b9a6b2cfc Kernel: Fix ext2 file write 2023-09-11 01:20:39 +03:00
Bananymous f178fa6d5b BuildSystem: remove install-usb.sh since it is not used
This was dangerous script since it rewrote /dev/sda
2023-09-10 01:20:44 +03:00
Bananymous 9ebf29991d Kernel: Add basic dd command
This only supports if, of, bs, count and status=progress
2023-09-10 01:19:47 +03:00
Bananymous 8fb5f97a18 Kernel: Improve open() POSIX compatability
Also rename Process::sys_creat -> create_file since it is not
actually a syscall and only used by open as a healper.
2023-09-10 00:31:42 +03:00
Bananymous 921d95d18f All: Clear lines with only whitspace in them 2023-09-10 00:31:42 +03:00
Bananymous 1fcf122c50 Kernel: Add basic ZeroDevice to /dev/zero 2023-09-10 00:31:42 +03:00
Bananymous dd9af56e21 Kernel: Start work on making inodes more thread safe
All inode operations are now locked and thread blocked
2023-09-10 00:31:42 +03:00
Bananymous 39a5c52088 Kernel: Fix directory permissions
We did not care about X bit in directories and instead used only the
R bit for search/read.
2023-09-08 11:46:53 +03:00
Bananymous 660f7cbfeb Kernel: Ext2 directories can allocate new blocks if needed 2023-09-08 10:29:26 +03:00
Bananymous 1abf54d652 Kernel: Fix bug in disk writing
I have used two weeks in locating a bug in my ext2 implementation
while the bug was actually in disk write. If you called write_sectors
on disk it would write the first sector_size bytes repeatedly to all
asked sectors and this corrupted the disk...
2023-09-08 02:43:08 +03:00
Bananymous 3750d29b2b Kernel: Fix possible out-of-bounds disk access in ext2 2023-09-08 02:42:53 +03:00
Bananymous e75522a005 BuildSystem: Cleanup disk image creation 2023-09-08 02:41:39 +03:00
Bananymous 5972c73950 LibC: fix fread implementation
fread() should read until either size*nitems bytes are read or eof
is reached.
2023-09-07 16:00:47 +03:00
Bananymous 41757b5f6c Shell: Clean exit on ^D and use getchar()
Use getchar() instead of fread(&ch, 1, sizeof(char), stdin). This
is much cleaner.
2023-09-07 15:51:05 +03:00
Bananymous c33c7f8b3b Init: use read instead of fread()
This allows actually correct behaviour. My fread implementation is
flawed. It should not return on '\n'
2023-09-07 15:47:59 +03:00
Bananymous 4648f6718e Kernel: Cleanup TTY::read() 2023-09-07 15:27:21 +03:00
Bananymous b30af0edca Kernel: TTY now reads input byte by byte
This allows correct behaviour for character streams / keyboard
handling. Serial input can now send working ^C :D
2023-09-07 15:06:27 +03:00
Bananymous 5e1725abb2 Kernel: Remove bitmap debug printing from ext2 inode allocation 2023-09-06 01:25:09 +03:00
Bananymous 6ad2f23259 Kernel: Correct inode links count 2023-09-05 14:46:56 +03:00
Bananymous 0d725f68e3 Snake render grid lines without extra space after last '#' 2023-09-05 14:35:53 +03:00
Bananymous 0770ba0936 BuildSystem: Don't set permissions on image creation
This is already handled by *new* tar ball sysroot
2023-09-05 14:35:53 +03:00
Bananymous c13b5aecfe Snake: Reorder apple generation/snake body update
If new apple was generated to heads previous position after update,
it would not render.
2023-09-05 14:30:30 +03:00
Bananymous 3892579049 Base: Add grub entries for serial-only boot
We can now fully boot with serial input and output :)
Qemu can be run with -nographic flag
2023-09-05 01:07:52 +03:00
Bananymous ba7e1b9ca5 Kernel: Add /dev/tty symlink that targets the current tty 2023-09-05 01:07:52 +03:00
Bananymous f0772e385c Kernel: Implement basic RamSymlinkInode 2023-09-05 01:07:52 +03:00
Bananymous 3fd94b1acb Kernel: You can now read serial output from the /dev/ttyS* 2023-09-05 01:07:52 +03:00
Bananymous 93c5755012 Kernel: Receive interrupts for COM1 and COM2 input
The input has to still be attached to terminal
2023-09-04 22:07:40 +03:00
Bananymous 323de3c866 Kernel: Rework whole Terminal structure
Serial monitors can now be used as a output. This requires editing
init code for the stdio opening. Serial input is not supported, so qemu
still needs graphical window for ps/2 keyboard.
2023-09-04 19:34:18 +03:00
Bananymous 52b9fddfd7 Kernel: Make serial devices to their own class 2023-09-04 14:57:05 +03:00
Bananymous e4041ce5ec Userspace: Implement basic snake game :) 2023-09-04 14:30:45 +03:00
Bananymous 04cfbca336 Kernel: Add ANSI support for hiding/showing cursor to TTY 2023-09-04 14:30:09 +03:00
Bananymous 7184514b5d LibC: add time() implementation 2023-09-04 13:52:58 +03:00
Bananymous dfb18d38f7 LibC: Add rand() and srand() implementation
This code is from the wikipedia page for Permuted congruential generator
2023-09-04 13:52:13 +03:00
Bananymous 9901f95d78 Kernel: Add basic nanosleep, only millisecond percision 2023-09-04 12:59:50 +03:00
Bananymous b2139c0b1e Kernel: Add basic support for O_NONBLOCK (only for tty) 2023-09-04 12:57:52 +03:00
Bananymous 6f002c926a Kernel: add basic fcntl() with couple of commands and no validation 2023-09-04 12:57:09 +03:00
Bananymous a711462ef4 Kernel: Split ext2 implementation to multiple files 2023-09-01 15:10:23 +03:00
Bananymous f31c6b847f Kernel: Start work on writable ext2 filesystem 2023-08-31 23:40:31 +03:00
Bananymous 195ccf4f53 Kernel: Add missing ififo() to Inode::Mode 2023-08-31 21:38:31 +03:00
Bananymous 5786ffe7d2 BuildSystem: Add cmake target to validate main partition 2023-08-31 21:37:30 +03:00
Bananymous 073edd0b8e Kernel: Stack trace dumping validates pointers before using them 2023-08-31 21:36:23 +03:00
Bananymous 2c136dae2d Update README.md 2023-08-31 17:54:12 +03:00
Bananymous 250789aa20 Kernel: better handle kernel errors 2023-08-29 00:13:21 +03:00
Bananymous 8032824054 BuildSystem: use -a with rsync
This allows cmake to not rebuild whole project every time
2023-08-28 11:38:17 +03:00
Bananymous 7aaea786c2 LibC: Don't undef needed values in inttypes.h 2023-08-24 15:48:14 +03:00
Bananymous ce8e8e68f4 BuildSystem: Add USES_TERMINAL when preparing sysroot
You were not able to enter sudo password on sysroot unpacking,
which caused ninja to hang
2023-08-23 20:48:40 +03:00
Bananymous 80e7a89f67 BuildSystem: Base sysroot is now distributed as a tar ball
This allows file and directory permissions work as intended.

cmake is now filled with 'sudo' but with sudo timeout this should be
fine.
2023-08-23 10:38:21 +03:00
Bananymous b780df8be0 Shell: hostname is not parsed from /etc/hostname 2023-08-23 10:38:21 +03:00
Bananymous c4210b5810 Shell: use process groups more properly 2023-08-22 14:54:50 +03:00
Bananymous 38e72019c7 Kernel: kill() with negative pid actually matches pgid 2023-08-22 14:54:15 +03:00
Bananymous d745fca86a Kernel: Process keeps track if forked process has called exec*() 2023-08-22 14:53:46 +03:00
Bananymous 60a2185ee6 Kernel/LibC: implement proper getpgid and setpgid 2023-08-22 14:53:12 +03:00
Bananymous d634fec8dc Kernel: Add function to enumerate processes in session 2023-08-22 14:52:28 +03:00
Bananymous e33bf62bba BAN: increase function size to 5 * sizeof(void*) 2023-08-22 14:52:03 +03:00
Bananymous 2dcd4ed131 Shell/init: We now use pgrp instead of pid and init open terminal 2023-08-22 11:37:04 +03:00
Bananymous c9243f0d1e Kernel/LibC: Add {get,set}pgrp() 2023-08-22 11:36:33 +03:00
Bananymous 0f6c19a1b7 Kernel: Barebones implementation sessions and process groups 2023-08-22 11:35:40 +03:00
Bananymous 642929f071 BAN: Add enum class Iteration for for_each loops 2023-08-22 11:30:53 +03:00
Bananymous 2746419c8c BAN: Function call is now const 2023-08-22 11:30:40 +03:00
Bananymous b5a7246ba7 LibC: make printf buffer only 128 bytes
Only reason for it to be big is if you want super large zero padding
for your number. This will eventually be handled without the buffer.
2023-08-18 16:49:27 +03:00
Bananymous c1d82282d9 Id: Print username corresponding to uid/euid
Also print euid and egid if they differ from uid or gid.
2023-08-18 15:36:51 +03:00
Bananymous 0abe30af38 BuildSystem: only apply stack usage warning to libc 2023-08-17 20:49:52 +03:00
Bananymous 089da2608c Shell: command execution and parsing support piped commands
There is still problems with "blocking" builtin commands (time),
return value, ...
2023-08-17 12:05:38 +03:00
Bananymous 85c6149138 LibC: add fileno() and fdopen()
fdopen() doesn't currently care about mode and will have same mode
as the underlying file descriptor.
2023-08-17 12:03:59 +03:00
Bananymous 79f3aa5419 Kernel/LibC: add dup() syscall and function 2023-08-17 12:03:29 +03:00
Bananymous ed5f4d64a8 Shell: add basic printf test 2023-08-16 10:49:34 +03:00
Bananymous 569e76a848 LibC: printf handles nan and inf 2023-08-16 10:49:34 +03:00
Bananymous 3a36c30e80 LibC: math.h defines is*() macros and uses builtins for values 2023-08-16 10:41:55 +03:00
Bananymous a09232a555 cat: use 1025 buffer. this allows reads to be 1024 byte
reading from disk currently returns ENOTSUP if buffer size is not
multiple of sector size.
2023-08-16 09:33:14 +03:00
Bananymous 5a94818210 Shell: $? returns last return value 2023-08-15 09:17:46 +03:00
Bananymous 2441f208c6 Userspace: Add basic whoami command 2023-08-15 09:03:51 +03:00
Bananymous db2eca697e Shell: '\u' in PS1 is replaced with username corresponding to euid 2023-08-14 14:55:23 +03:00
Bananymous 81d79cca61 Shell: ^A moves cursor to the beginning of line 2023-08-14 12:26:22 +03:00
Bananymous 871c792976 Kernel: O_SEARCH doesn't require any permissions
Except read permissions for all directories in the path

This allows us to stat e.g. device files for which we don't have
the permissions.
2023-08-11 12:26:07 +03:00
Bananymous 9ab7e76a3b LibC: cleanup fstatat 2023-08-11 12:25:15 +03:00
Bananymous 8480ffe108 Kernel: open() now validates file access mode 2023-08-11 11:53:38 +03:00
Bananymous e1400f9680 Kernel: Remove unused syscall 2023-08-11 11:43:48 +03:00
Bananymous 77ff585248 Userspace: implement basic stat
I tried to format the output pretty much how linux does, except for
uid and git -> name resolution which is not implemented
2023-08-11 10:30:50 +03:00
Bananymous 9cb50cba33 LibC: Fix S_IF* macros and add macros for access/type masks 2023-08-11 10:30:50 +03:00
Bananymous 6e5bce3c57 Kernel: Zero initialize threads sse_storage 2023-08-11 00:26:43 +03:00
Bananymous f75adab9d8 Kernel: Move structures in boot.S to .data section 2023-08-11 00:26:12 +03:00
Bananymous 5d0a6e7b08 Kernel: HPET is now used in legacy mode when PIC is forced 2023-08-10 22:01:30 +03:00
Bananymous b0c8a9cdc4 Kernel: Fix slave PIC
we dont mask interrupt 2 in PIC since it corresponds to the
slave PIC. Also cleanup PIC code :)
2023-08-10 21:52:31 +03:00
Bananymous ebe0adb3b5 Kernel: HPET doesn't use the legacy mapping anymore 2023-08-10 21:08:32 +03:00
Bananymous 4842d5e2b9 Kernel: APIC now uses MMIO namespace functions for mmio 2023-08-10 21:07:23 +03:00
Bananymous 0b8396b1a6 Kernel: IDT now sends EOI for every interrupt
EOI is sent before calling the interrupt handler. This should be fine
since all interrupts are processed with interrupts disabled
2023-08-09 09:57:02 +03:00
Bananymous 55ef793a74 Kernel: Validate HPET tick period 2023-08-09 09:50:38 +03:00
Bananymous 96c0ad0d3d Kernel: Remove get_unix_timestamp from SystemTimer
Kernel now uses the percise real_time() and time_since_boot()
2023-08-09 08:57:50 +03:00
Bananymous 6fcb191ca0 Kernel: Add PageTable::Flags::CacheDisable
Also fix multiple places where we were using uint8_t as page table
flags instead of PageTable::flags_t which we promoted to uint16_t
while back.
2023-08-06 23:59:30 +03:00
Bananymous 3df97c36e6 Kerne: SystemTimer can now output the current real time 2023-08-04 16:06:47 +03:00
Bananymous c732297623 Kernel: Rename TimerHandler to SystemTimer
I changed SystemTimer to only handle the "best" supported timer
it can initialize.
2023-08-04 16:06:47 +03:00
Bananymous ca5a097ef5 Kernel: Add function to retrieve boot time as timespec 2023-08-04 16:06:47 +03:00
Bananymous 1f8a5f0ce5 Kernel: F11 now prints current time to debug output
This allows better timer percision and system freeze checks
2023-08-04 15:22:51 +03:00
Bananymous 1fa7a1cac4 Kernel: Add basic HPET support to replace PIT if exists
This works same way as the PIT implementation; calls Scheduler every
milli second.
2023-08-04 15:22:51 +03:00
Bananymous ea4a70c3b3 Kernel: Move sleep() implementation to TimerHandler 2023-08-04 15:15:00 +03:00
Bananymous aa0929614a Kernel: Add more structures to ACPI 2023-08-04 15:13:47 +03:00
Bananymous 3c31fc9c4b Kernel: Implement basic MMIO functions
This is equivalent to IO.h except for memory mapped io instead of
IO ports
2023-08-04 15:12:29 +03:00
Bananymous 7eb2c140fe Kernel: Move RTC to Timer directory
PIT ms counter seems to be off by multiple seconds/minute. I will
be probably changing to HPET for system time
2023-08-04 11:12:16 +03:00
Bananymous 659adb89a6 Kernel: Start work on abstracting Timers 2023-08-04 10:29:42 +03:00
Bananymous dd17124c77 Kernel: Remove unnecessary timer check from Scheduler 2023-08-04 10:29:42 +03:00
Bananymous 453a5387cb BAN: UniqPtr can now be constructed from other convertible UniqPtr 2023-08-04 10:29:42 +03:00
Bananymous 643e87a076 Kernel: Threads are deleted sooner and cleaner
We now delete threads when
    1. it is marked as Terminated and is the current thread
    2. it tries to start execution in Terminated state

This allows us to never have thread executing in Terminated state
2023-08-04 10:29:42 +03:00
Bananymous 20eafb4cc4 Kernel: align userspace stacks
I was getting #GP on sse instructions
2023-08-03 18:09:48 +03:00
Bananymous e715d52f80 LibC: fix typo 2023-08-02 22:10:12 +03:00
Bananymous 3139391e06 Kernel: Change PageTable API
Getting free pages not reserves them, so you don't have to hold
the page table lock :)
2023-08-02 22:09:14 +03:00
Bananymous 64ad752e73 Kernel: PageTable now supports reserved pages
You can now map pages as reserved, so that they will not be given
from get_free_page() or get_free_contiguous_pages().
2023-08-01 16:35:54 +03:00
Bananymous dddfa308d7 Shell: Print if the process exited because of a signal 2023-08-01 14:24:36 +03:00
Bananymous a78a7ed156 Kernel: Cleanup returns from any kind on interrupts 2023-08-01 14:23:50 +03:00
Bananymous b4eddf04c4 LibC: sys/wait.h now has proper exit status macros
We use the same format as basically every implementation
2023-08-01 10:32:04 +03:00
Bananymous 2a851b52f1 BuildSystem: Edit build flags regarding sse and warnings 2023-07-31 22:31:17 +03:00
Bananymous b245a55ea0 Kernel: store/load sse/mmx/fpu state on isr/irq/syscall
I haven't tested this yet but should be fine. This will be optimized
to only save state from threads that are using it
2023-07-31 22:28:57 +03:00
Bananymous c4f6c859c1 Kernel: Generally improve signals 2023-07-31 22:28:18 +03:00
Bananymous 834bf33e57 LibC: we don't parse syscall arguments in unistd
We just call Kernel::syscall() with 5 arguments from the variadic function.

This was a necessary addition since the syscall() function
used over 2 KiB of stack space.
2023-07-31 22:24:11 +03:00
Bananymous 1a6100f083 Kernel: Cleanup signal trampoline 2023-07-30 15:58:35 +03:00
Bananymous de927b6b05 Kernel: Remove is_in_syscall from Thread 2023-07-30 14:49:51 +03:00
Bananymous 1d7795e22c Kernel: Hardware exceptions now sends signals to userspace 2023-07-30 14:34:15 +03:00
Bananymous abe8810d47 Kernel: raise() now force sends signal 2023-07-30 14:17:07 +03:00
Bananymous acf125c853 Kernel: Signals are not queued anymore
Posix doesn't require signal queing if you don't use sigqueue() which
we don't support. Process also has its own pending signal mask.
2023-07-29 16:54:31 +03:00
Bananymous 925df39107 Kernel: Userspace sets the foreground process and Shell handles ^C 2023-07-28 18:10:36 +03:00
Bananymous 9279bbbd19 Kernel: TTY now sends SIGINT on ^C to foreground process 2023-07-28 18:10:09 +03:00
Bananymous 9fd9a8b5b1 Kernel: Mark reading section from StorageDevice as terminate critical 2023-07-28 18:09:35 +03:00
Bananymous 3c6be319b1 Kernel: Restructure process and thread termination
When we want to kill a process, we mark its threads as Terminating
or Terminated. If the thread is in critical section that has to be
finished, it will be in Terminating state until done. Once Scheduler
is trying to execute Terminated thread it will instead delete it.

Once processes last thread is marked Terminated, the processes will
turn it into a cleanup thread, that will allow blocks and memory
cleanup to be done.
2023-07-28 18:06:20 +03:00
Bananymous 104894c0c7 Kernel: Rewrite DiskCache
We now cache only clean pages since I don't want to think about
syncing the disk later.
2023-07-27 22:22:39 +03:00
Bananymous 2f52001c6d Kernel: Locks allow locking after locker is invalid
SpinLock and RecursiveSpinLock will now allow locking after the initial
locker is invalid. This allows us to kill threads even if they are holding
internal locks
2023-07-27 18:52:51 +03:00
Bananymous f609170a6a Kernel: DiskCache uses are now done without interrupts
This allows disk cache to not be invalid state after partial read/write
2023-07-27 18:52:51 +03:00
Bananymous 9f75b04714 Kernel: Scheduler can now check if tid is valid
Tid can become invalid if the thread is already terminated
2023-07-27 18:34:06 +03:00
Bananymous 7b4a2fe3d1 Kernel: Process::exit() unblocks the whole lock before blocking 2023-07-24 22:29:59 +03:00
Bananymous f6ee4b3496 Kernel: Implement SYS_GET_PID and SYS_TCSETPGID
we don't have consept of process groups yet
2023-07-24 22:29:59 +03:00
Bananymous 1ef0534b69 Kernel: TTY now stores foreground process pid
this will be process pgid when we have process groups
2023-07-24 22:29:59 +03:00
Bananymous b9dd1895bb Kernel: add Thread::queue_signal() 2023-07-24 22:26:10 +03:00
Bananymous be47743dfa Kernel: Scheduler can now block threads based on tid 2023-07-24 22:24:21 +03:00
Bananymous 57798e5d76 Kernel: remove Semaphore::is_blocked
this will not mean anything after semaphores can spuriously wake up
2023-07-24 22:23:13 +03:00
Bananymous cd64c1cfec Kernel: add is_tty() to inode 2023-07-24 22:19:59 +03:00
Bananymous f76d921e25 Kernel: you can now ask process if it is userspace process 2023-07-23 18:54:10 +03:00
Bananymous a2bf474013 Kernel: OpenFileDesctiptor can now return inode of fd 2023-07-23 18:52:33 +03:00
Bananymous 9729e5a05b Kernel: Change signal kernel API
return rsp and rip are now stored per thread
2023-07-23 18:33:10 +03:00
Bananymous 2dce0a0415 Kernel: Userspace signal handlers are now called one at a time
I added a syscall for telling the kernel when signal execution has
finished. We should send a random hash or id to the signal trampoline
that we would include in the syscall, so validity of signal exit can
be confirmed.
2023-07-23 13:34:53 +03:00
Bananymous d560137ae6 Kernel/LibC: add SYS_SIGNAL/signal() 2023-07-21 20:08:13 +03:00
Bananymous c12f4fb40f Kernel: Make signals more POSIX 2023-07-21 20:01:12 +03:00
Bananymous 10169d773d Kernel/LibC: Add SYS_KILL/kill() 2023-07-21 19:27:38 +03:00
Bananymous b78596dcf4 Kernel: Scheduler now sends queued signals 2023-07-21 19:27:10 +03:00
Bananymous 40f7c6b8fa Kernel: Make signals thread specific 2023-07-21 19:00:59 +03:00
Bananymous 8063700d7b Kernel: add default signal actions 2023-07-21 18:02:35 +03:00
Bananymous 70f89ed1fc BuildSystem: Strip kernel. We will add the map once we use it 2023-07-21 15:47:31 +03:00
Bananymous c2cf98e32f Kernel/LibC: Add bareboness signals
You can now call raise() to raise a signal. Signal handlers are
not yet supported, but the handling works :)
2023-07-21 15:45:02 +03:00
Bananymous a1db032ba9 Kernel: kernel memory takes now full pml4e
This allows flags to work properly
2023-07-21 13:47:05 +03:00
Bananymous 5babd7b4f2 Kernel: remove PAGE_FLAGS_MASK as that was not correct anymore
After I added NXE bit, the flags mask is no longer valid
2023-07-21 12:01:50 +03:00
Bananymous c0029a2fa2 Kernel: Don't map kernel executable memory as writable 2023-07-21 11:17:39 +03:00
Bananymous 4e35b8b49d Kernel: Syscalls now get the interrupt stack 2023-07-21 11:04:16 +03:00
Bananymous 21c6135ae2 BAN: Fix function call in Optional 2023-07-21 10:57:46 +03:00
Bananymous b02c486ad0 Kernel: add NullDevice to /dev/null 2023-07-20 00:06:22 +03:00
Bananymous 08cd285ca6 Kernel: move Device.h to its own directory 2023-07-19 23:55:38 +03:00
Bananymous 94af856db0 Kernel: cleanup sys_exec()
We now scope everyting so desctructors get called
2023-07-19 23:20:39 +03:00
Bananymous bf5d74b8bc Kernel: fix memory leak in PageTable
PageTable used to leak 2 pages on creation and other two on deletion
2023-07-19 23:17:39 +03:00
Bananymous fd9210ba9e Kernel: FixedWidthAllocator creation can now fail 2023-07-19 18:07:24 +03:00
Bananymous 66d9260257 Kernel: VirtualRange creation can fail 2023-07-19 17:56:26 +03:00
Bananymous 5d2bfc858e Kernel: SYS_FORK can now fail instead of panicing on error 2023-07-19 17:47:12 +03:00
Bananymous 8b34880064 Kernel: Fix bug which made bochs unbootable
We could not boot if ATABus did not have valid primary device.
2023-07-13 15:53:09 +03:00
Bananymous 98c698d9ec Kernel: Enable global bit on kernel pages 2023-07-13 15:23:25 +03:00
Bananymous 9ea2c1d932 Kernel: Support execute disable bit
We will now map executable memory explicitly as executable.
2023-07-13 14:28:53 +03:00
Bananymous 297e65f19a Kernel: CPUID can detect wether cpu supports nxe bit 2023-07-13 14:24:58 +03:00
Bananymous 65559a3e44 Kernel: Fix multiple bugs with terminal 2023-07-13 13:09:52 +03:00
Bananymous c3a71e94a9 Kernel: Fix ATA disk and partition numbering 2023-07-13 12:12:47 +03:00
Bananymous fb4b363a16 Init: Use the new pwd.h api for user parsing 2023-07-13 12:01:41 +03:00
Bananymous 4154f43b49 LibC: add getpwname() and getpwuid() 2023-07-13 12:01:16 +03:00
Bananymous b1fcb0b58f LibC: implement endpwent(), getpwent() and setpwent() 2023-07-13 11:22:09 +03:00
Bananymous 00409ffa60 Kernel: Ext2Inode::create_file actually sets the inode data
We used to just create inode without touching its memory.
2023-07-13 10:20:56 +03:00
Bananymous 16acd50559 BAN: Iterators are now single general type with CONST template
This allows us to use the same base class for iterator and
const_iterator which were practically the same code.
2023-07-12 13:35:21 +03:00
Bananymous 5df48804e1 BAN: add either_or and either_or_t
This allows you to select type depending on constexpr evaluatable
boolean
2023-07-12 13:34:31 +03:00
Bananymous 9eab6710ce BAN: Implement ConstIteratorDouble and add it to HashMap
This is same as IteratorDouble except it uses const_iterator and does
not return non-const references.
2023-07-12 11:41:05 +03:00
Bananymous 65424a6769 BAN: Generalize HashMapIterator to IteratorDouble
This iterator should be able to iterate any container within container
with type iterator defined.

This also fixed bug if first entry in outer container is empty container.
2023-07-12 09:29:05 +03:00
Bananymous 12d53ac233 Base: add empty directories with .gitkeep to base/ 2023-07-11 08:02:28 +03:00
Bananymous 9bcfb34524 Kernel: Rewrite whole device structure
We now have DevFileSystem which is derived from RamFileSystem. All
devices are RamInodes. We don't have separate DeviceManager anymore.
To iterate over devices, you can loop througn every inode in devfs.
2023-07-10 23:17:14 +03:00
Bananymous f88b9ae4f2 BAN: Add iterators to HashMap 2023-07-10 23:16:41 +03:00
Bananymous 8cd91f5a6a Userspace: add basic 'touch' command 2023-07-10 16:38:15 +03:00
Bananymous f65e5f4190 tee: indent with tabs 2023-07-10 16:18:08 +03:00
Bananymous f521a98157 Userspace: Add basic tee command 2023-07-10 16:07:53 +03:00
Bananymous ee4ef6638c Kenrel: RamInode now implements truncate() 2023-07-10 16:07:09 +03:00
Bananymous 0910958c04 Kernel: OpenFileDescriptors can now store more than 8 bits of flags 2023-07-10 16:06:36 +03:00
Bananymous d6408bcf17 Kernel: add O_TRUNC
this is not supported by anything yet
2023-07-10 15:48:18 +03:00
Bananymous 178fc00905 Kernel: creat() mode now has to be access mode
We provide the S_IFREG in creat
2023-07-10 15:34:41 +03:00
Bananymous 89d4fa4d9b Kernel: Ext2 fill now return ENOTSUP on write
We used to crash the kernel
2023-07-10 15:34:25 +03:00
Bananymous 46e1419e70 Kernel: O_APPEND is now supported 2023-07-10 15:11:27 +03:00
Bananymous f7c4bc908e Kernel: open() and openat() now take mode as parameter
O_CREAT now tries to create file if O_CREAT is specified
2023-07-10 15:08:54 +03:00
Bananymous a0ecbed726 Kernel: You can now read/write to RamInodes
RamFS should be stored on physical pages and not in kmalloc, but
that will be implemented later :)
2023-07-10 14:09:35 +03:00
Bananymous 74fc0aa308 Kernel: Inode::create_file() now takes uid and gid as parameters 2023-07-10 13:32:10 +03:00
Bananymous d5f0448e48 Kernel: start work on ram file system 2023-07-10 13:26:14 +03:00
Bananymous 51e4b11890 LibC: add NAME_MAX to limits.h
This is defined to 255 which is _XOPEN_NAME_MAX, smallest value
for XOPEN compliance
2023-07-10 11:48:11 +03:00
Bananymous d713f252aa Kenrel: Fix inode comparison
We should not compare rdevs
2023-07-10 11:48:11 +03:00
Bananymous 5ec2d85081 Kernel: Inode rename directory functions 2023-07-10 11:48:11 +03:00
Bananymous 0d132ee518 Kernel: Mark Ext2 classes final 2023-07-10 11:48:11 +03:00
Bananymous fabbb9f531 BAN: RefPtr can be constructed from other types 2023-07-10 11:48:11 +03:00
Bananymous 80c8d52dc5 Kernel: add more functionality to PCI 2023-07-09 23:04:11 +03:00
Bananymous b6c4a2dbf1 Kernel: Edit lock scopes and make string copy able to fail 2023-07-07 23:12:19 +03:00
Bananymous a2f5ad7bed Kernel: Move open file descriptors to their own class
This simplifies code a lot :)
2023-07-07 23:11:37 +03:00
Bananymous a337f414fc LibC: limits.h now defined OPEN_MAX 2023-07-07 23:08:49 +03:00
Bananymous 404b3dd44c Shell: $(...) expansion works now :) 2023-07-06 23:22:57 +03:00
Bananymous d04b031e30 Shell: you can call Shell -c ... to invoke the shell as interpreter 2023-07-06 23:22:49 +03:00
Bananymous 07fec6e211 Kernel/LibC: add basic dup2 2023-07-06 23:17:54 +03:00
Bananymous 4cd72992c8 Kernel/LibC: Add basic pipe() syscall and command
You can now create pipes :)
2023-07-06 22:16:26 +03:00
Bananymous cdcb395640 LibC: add read() and write() to unistd 2023-07-06 22:15:55 +03:00
Bananymous 5a8eb51968 Kernel: TTY now unblocks semaphore after read
This allows concurrent reads not fully reading the buffer not block
indefinately.
2023-07-06 21:32:34 +03:00
Bananymous a74422281f Kernel: Add O_CLOEXEC 2023-07-06 20:00:33 +03:00
Bananymous a45f9ee76b Kernel: Remove spammy process/thread exit printing 2023-07-06 10:34:46 +03:00
Bananymous f19dc114d6 Userspace: Shell now has 'env' for printing environment 2023-07-06 10:32:43 +03:00
Bananymous d2aabb669b Userspace: Shell imporove 'time' command 2023-07-06 09:45:04 +03:00
Bananymous 9c3f4039a5 Userspace: Shell now has time builtin 2023-07-06 00:39:04 +03:00
Bananymous 1fb305fa45 Kernel/LibC: add clock_gettime() for CLOCK_MONOTONIC
This gets the number of milliseconds since boot
2023-07-06 00:38:29 +03:00
Bananymous 4086d7c3be Kernel: rework the whole PageTable structure
We now have page table structure for kernel memory which is shared
between all processes.
2023-07-05 23:41:35 +03:00
Bananymous 60fe5a656c LibC: Fix syscall SYS_READ and SYS_WRITE arguments 2023-06-19 10:38:29 +03:00
Bananymous 7d254c26bc Kernel: Rewrite and optimize DiskCache
DiskCache now consists of PageCaches which are caches of contiguous
sectors. This allows the disk cache to be ordered and faster traversal.

We seem to have a problem somewhere during reading. The stack gets
corrupted.
2023-06-19 10:31:47 +03:00
Bananymous 328d67f551 Userspace: u8sum fix error message 2023-06-19 10:31:23 +03:00
Bananymous 84ecf861cd Userspace: Shell now processes $ arguments 2023-06-19 01:39:24 +03:00
Bananymous 46a6daccfe Userspace: Shell argument parsing now appriciates quotes 2023-06-19 01:07:00 +03:00
Bananymous 3df3c37bad Userspace: Shell argument parse now results in BAN::String 2023-06-19 00:34:44 +03:00
Bananymous 191a24110a Userspace: Shell now sets SHELL environment variable 2023-06-18 23:35:51 +03:00
Bananymous 275a730485 LibELF: We use BAN::Vector<uint8_t> as elf storage
This is made possible by the dynamic kmalloc
2023-06-18 23:29:23 +03:00
Bananymous 9a7b2587af Kernel: kmalloc has now somewhat dynamic storage
Allocations bigger than PAGE_SIZE and those not forced to be identity
mapped are now done on a GeneralAllocator. This allows us to use kmalloc
for big allocations; bigger than the fixed 1 MiB storage.

This is still a hack and the whole kmalloc will have to be rewritten at
some point, but for now this does the job :D
2023-06-18 23:27:00 +03:00
Bananymous 09c824b90e Kernel: Add GeneralAllocator::paddr_of
Yoy can now query physical address of a virtual address for general
allocator allocation
2023-06-18 23:25:51 +03:00
Bananymous 57e59d998f Kernel: Add enum for ISR name to number 2023-06-18 23:24:27 +03:00
Bananymous 44fb3945df Kernel: General allocator takes first valid vaddr as parameter 2023-06-17 22:23:34 +03:00
Bananymous 95c4e608de Kernel: Move print during boot 2023-06-12 23:45:36 +03:00
Bananymous b9603d9d23 BAN: Optional can be constructed inplace 2023-06-12 23:45:36 +03:00
Bananymous 16e5f96b1d BAN: Add operator-> and operator* to Optional 2023-06-12 22:25:14 +03:00
Bananymous d0a0e3bdef Userspace: Add u8sum
This program caluculates the sum of bytes in file mod 256
2023-06-12 20:36:16 +03:00
Bananymous 5dcc53bcf2 Kernel: Make disk IO blocking thread safe
This was causing a lot of deadlocks on vms without kvm accel
2023-06-12 18:57:47 +03:00
Bananymous 23543b15ca Kernel: Rework syscall calling
I removed the intermediate function when calling syscalls. Now syscall
handler calls the current process automatically. Only exception is
sys_fork, since it needs a assembly trampoline for the new thread.
2023-06-12 14:16:48 +03:00
Bananymous 2253c45feb Kernel: Remove Shell from kernel
This is now obsolete since we have a userspace Shell.
2023-06-12 02:04:52 +03:00
Bananymous 9f9a70713e Base: add home directories for root and user 2023-06-12 02:03:13 +03:00
Bananymous cd57adc856 BuildSystem: every file except /home/* is now owned by root 2023-06-12 02:03:13 +03:00
Bananymous b4b892148c Userspace: Shell processes PS1 '\~' as cwd and implement cd 2023-06-12 02:03:13 +03:00
Bananymous 048183ddb5 Userspace: init now sets HOME environment variable and cd's into HOME 2023-06-12 02:02:52 +03:00
Bananymous f09b82c4b5 Kernel/LibC: add SYS_{SET,GET}_PWD and chdir, getpwd 2023-06-12 02:02:52 +03:00
Bananymous 7aeb8e4d36 Kernel: allocate thread stacks from 0x300000 onwards
I had a problem where thread stack was overlapping with elf loading
2023-06-12 01:02:19 +03:00
Bananymous 58ec4d6a31 Kernel: Fix bug in elf loading
We were allocating one extra page
2023-06-12 00:59:19 +03:00
Bananymous 25f8343269 Userspace: init now default logins as user 2023-06-12 00:46:07 +03:00
Bananymous e9cb844c28 Userspace: Shell now uses PS1 as prompt if set 2023-06-12 00:45:47 +03:00
Bananymous 30ac046232 Userspace: Add color to ls 2023-06-11 23:00:19 +03:00
Bananymous f67f8ccbe0 Userspace: init now sets user and group ids before running their shell 2023-06-11 22:37:00 +03:00
Bananymous e9217b3484 Userspace: Add basic id that prints {,e}{uid,gid} of the current proc 2023-06-11 22:37:00 +03:00
Bananymous 11b68f6a53 Kernel: Add SYS_GET_{,E}{UID,GID} 2023-06-11 22:37:00 +03:00
Bananymous 3fe67e4882 Kernel: Add syscalls for set{,e,re}{uid,gid} 2023-06-11 22:37:00 +03:00
Bananymous 9288537949 LibC: gid_t and id_t are now signed types
I have no idea why I had made them unisigned
2023-06-11 22:37:00 +03:00
Bananymous 78536f9678 Kernel/Userspace: Add basic init process
This process parses /etc/passwd and promps login screen.
When an username is entered, it will launch that users shell
2023-06-11 22:37:00 +03:00
Bananymous cce2f3e19a BAN: Add basic Optional 2023-06-11 21:00:25 +03:00
Bananymous 297141f321 LibC: add strchrnul()
this is a gnu libc extension
2023-06-11 20:18:03 +03:00
Bananymous c7ec19c25c Kernel: Add basic Credentials for the system
Now filesystem access/open, etc confirm that you have access for rwxs
2023-06-11 20:06:06 +03:00
Bananymous 3181ea7b4d Kernel: GeneralAllocator and FixedWidth allocator invalidate TLB caches
We were getting random exceptions when reallocating same addressess and
this fixes that problem :)
2023-06-11 15:57:48 +03:00
Bananymous 1cf464d5c7 Userspace: add basic ls command 2023-06-11 03:38:44 +03:00
Bananymous 83a6e6f637 LibC: Reorder some syscalls 2023-06-11 03:29:22 +03:00
Bananymous 64890bb640 Kernel: Add SYS_OPENAT 2023-06-11 03:29:22 +03:00
Bananymous caca6dc701 Kernel: Exceptions will now enable interrupts before calling exit()
this allows scheduler verification on reschedule() not panic
2023-06-11 03:29:22 +03:00
Bananymous 68ec443e07 LibC: implement close 2023-06-11 03:29:22 +03:00
Bananymous aa86125f2b Kernel: Implement SYS_FSTAT 2023-06-11 03:29:22 +03:00
Bananymous c423164066 LibC: Implement basic dirent functionality
We don't currently support seeking
2023-06-11 03:29:22 +03:00
Bananymous 14aeb8de10 Kernel: we don't panic anymore on unrecognized syscall 2023-06-11 00:18:48 +03:00
Bananymous 691b17e0cc Kernel: allow open() call with O_SEARCH 2023-06-11 00:18:34 +03:00
Bananymous eae6119039 Kernel: Add syscall for reading directory entries 2023-06-11 00:18:08 +03:00
Bananymous 30f81e1696 LibC: add missing O_EXEC and O_SEARCH 2023-06-11 00:17:18 +03:00
Bananymous 01311b470f Kernel: Rewrite directory listing so it can be integrated to libc 2023-06-11 00:17:18 +03:00
Bananymous 223d2ac3a6 Userspace: fix return values of cat and echo 2023-06-10 17:34:10 +03:00
Bananymous 4cdeb98897 Kernel: We now store the processes exit code 2023-06-10 17:31:56 +03:00
Bananymous 47c69e9def Userspace: add exit to shell 2023-06-09 01:51:23 +03:00
Bananymous 9728947d5d Userspace: add echo 2023-06-09 01:50:18 +03:00
Bananymous 66fe48e94b Userspace: add ^L support for shell 2023-06-09 01:24:33 +03:00
Bananymous c0fe4756cb Kernel: SpinLocks now reschedule if they cannot aquire the lock
This allows us to not actually spin doing nothing while waiting for
another (not executing) to release the lock. This api won't probably
work when we get to SMP
2023-06-09 00:53:32 +03:00
Bananymous 5032e79be3 Kernel: Process::exit() now uses the new Scheduler::reschedule()
We use this new function while waiting for all blocking threads to
resume execution
2023-06-09 00:49:19 +03:00
Bananymous 1aec3fcedd Kernel: Scheduler now has reschedule()
This can be called from anywhere and just causes the scheduler to
schedule the next thread. This is more efficient and verbose version
of Scheduler::set_current_thread_sleeping(0), since we don't have
to wake other threads or do other verifications.
2023-06-09 00:47:17 +03:00
Bananymous aec63081e2 Kenrel: Rename Scheduler::reschedule -> Scheduler::timer_reschedule 2023-06-09 00:41:43 +03:00
Bananymous 801025ad7b Kernel: Big commit. Rewrite ELF loading code
We now load ELF files to VirtualRanges instead of using kmalloc.
We have only a fixed 1 MiB kmalloc for big allocations and this
allows loading files even when they don't fit in there.

This caused me to rewrite the whole ELF loading process since the
loaded ELF is not in memory mapped by every process.

Virtual ranges allow you to zero out the memory and to copy into
them from arbitary byte buffers.
2023-06-09 00:37:43 +03:00
Bananymous 59b10c4d25 Kernel: PageTableScope locks the PageTable before disabling interrupts
This allows replacing some PageTableScopes with PageTable::lock()
2023-06-09 00:34:41 +03:00
Bananymous 841584ccbd Kernel: get_free_contiguous_pages works with non-page aligned addresses
also fix bug in ordering
2023-06-06 02:03:23 +03:00
Bananymous 24993f6020 LibC: implement setenv, unsetenv, putenv 2023-06-05 22:51:02 +03:00
Bananymous 55ea5c5488 Kernel: add basic support for environment variables
exec functions will search files from PATH
2023-06-05 22:51:02 +03:00
Bananymous 290b81dedc BAN: add StringView::contains(char) 2023-06-05 22:51:02 +03:00
Bananymous c6b5cc1e07 Kernel: exec now has better posix errors 2023-06-05 21:12:08 +03:00
Bananymous 357081346e Shell: load old termios for process execution 2023-06-05 21:12:08 +03:00
Bananymous 896b919c9d Kernel/LibC: pass environ pointer to process 2023-06-05 21:12:08 +03:00
Bananymous 6b73f4d187 Kernel: TTY now actually flushes on ^D 2023-06-05 20:21:46 +03:00
Bananymous 08a806e75d Kernel: verify that loaded elfs are executable 2023-06-05 19:29:32 +03:00
Bananymous 73c11c3d29 Shell: add bareboness utf8 support
This should work as long as TTY provides only valid utf8.
If the utf is invalid, assertion fails and the shell dies.
2023-06-05 18:55:22 +03:00
Bananymous 5af77dcfb9 Shell: we now support left/right arrows 2023-06-05 18:24:41 +03:00
Bananymous a7dc7ecb90 LibC: abort now prints 'abort()' and exits
we used to call assert in abort which then recursively called
abort again.
2023-06-05 18:23:19 +03:00
Bananymous cf88142856 Kernel: TTY now supports CSI s and u
These are commonly supported values to save and load cursor
position
2023-06-05 18:19:13 +03:00
Bananymous efb355c7be BuildSystem: disable nls from gcc 2023-06-05 17:55:47 +03:00
Bananymous d1ad38c8d4 Kernel/LibC: add SYS_STAT and stat(), lstat() 2023-06-05 14:37:14 +03:00
Bananymous 69b94dad00 Base: revert bin and lib symlinks to relative paths
Absolute paths fucked up toolchain building and usespace linking,
since g++ was finding host libraries from /usr/lib.
2023-06-05 10:08:01 +03:00
Bananymous 4c40aa6b38 Userspace: Start work on proper shell 2023-06-05 01:42:57 +03:00
Bananymous 5cf3221364 Base: /lib and /bin are now absolute symlinks 2023-06-04 18:12:05 +03:00
Bananymous 40055f023c LibC: add wait and waitpid
Note that wait() doesn't work since only waiting for specified
pid is supported. wait() will just return -1 and set errno to
ECHILD.
2023-06-04 18:00:52 +03:00
Bananymous 9fbb2b9369 Kernel: Add SYS_WAIT
This syscall waits for a given pid.
2023-06-04 18:00:52 +03:00
Bananymous a3aca67eef Kernel: fork() now clones current thread
This is how posix specifies thread cloning during fork
2023-06-04 17:40:37 +03:00
Bananymous 977652ad57 Kernel: Process FixedWidthAllocators come now in 4 sizes 2023-06-04 01:26:43 +03:00
Bananymous 5bf7ca1c80 Kernel: Allocators are now stored in UniqPtr
This allows proper memory management, we had some memory leak
2023-06-04 01:25:57 +03:00
Bananymous 1b1f22c35e Kernel: make load_elf() its own function 2023-06-04 01:24:11 +03:00
Bananymous 479817231a Kernel: PhysicalRange maps its nodes to kernel vaddr space
This keeps the lower half of address space cleaner
2023-06-04 01:20:47 +03:00
Bananymous e3c5477df4 Kernel: reorder process exit steps 2023-06-04 01:19:04 +03:00
Bananymous b403155ca9 Kernel: You can specify first vaddr for getting free pages 2023-06-04 01:15:48 +03:00
Bananymous 9fd3111011 BAN: Add UniqPtr 2023-06-04 00:39:20 +03:00
Bananymous 5f339d585c Kernel: PAGE_FLAG_MASK is now only 0xF
We don't care currenly about anything but the last few bits
2023-06-03 20:08:13 +03:00
Bananymous b168462b43 Kernel: PageTable now has debug_dump
This dumps all the mapped pages and their flags
2023-06-03 20:08:13 +03:00
Bananymous 68691faca1 Kernel: boot.S is back to 2 MiB pages
bochs doesn't seem to support 1 GiB pages
2023-06-03 18:53:36 +03:00
Bananymous 1e075c248f BAN: Errors now includes assert.h 2023-06-03 18:53:05 +03:00
Bananymous d201e65810 BuildSystem: remove sse and sse2 from userspace 2023-06-03 16:05:32 +03:00
Bananymous 3a79880e69 LibC: printf string persision works now 2023-06-03 15:07:02 +03:00
Bananymous 9228df0f23 Kernel: Inode/Device detection is done with overridden bool functions 2023-06-03 13:28:15 +03:00
Bananymous 402ad85583 Kernel: Heap will return 0 if no free page is available 2023-06-03 02:55:31 +03:00
Bananymous 53e91ba98a Kernel: DiskCache will try to shrink_to_fit after cache cleanup 2023-06-03 02:55:22 +03:00
Bananymous fd153f3762 Kernel: DiskCache won't crash when running out of kmalloc memory 2023-06-03 02:36:20 +03:00
Bananymous 813d0f1b5f Kernel: add basic disk cache
ATADevices now add disk cache to themselves
2023-06-03 02:23:14 +03:00
Bananymous 2b901abfb1 Kernel: Shell 'memory' now prints heap memory usage 2023-06-03 02:22:18 +03:00
Bananymous d4289f9e74 Update README.md 2023-06-02 18:43:30 +03:00
Bananymous 3a5d6914ba Userspace: create_program creates proper cmake files again 2023-06-02 18:42:25 +03:00
Bananymous e96efea2fb BuildSystem: all scripts have now bash shebang
I could not use the scripts on debian
2023-06-02 18:41:45 +03:00
Bananymous 2df7f8a1e5 BuildSystem: add rule to build libstdc++
We can now build libstdc++ and actually link with g++
2023-06-02 18:39:42 +03:00
Bananymous 8645c4c653 Kernel: Shell ls and stat now properly show symlinks 2023-06-02 18:22:56 +03:00
Bananymous 88e92eec9e LibC: Add simple definition for realloc
The syscall just crashes the kernel currently. I will implement
this when needed
2023-06-02 17:56:13 +03:00
Bananymous a41b8e416f LibC: add __cxa_at_exit() for libc 2023-06-02 17:50:55 +03:00
Bananymous 8716c8baf4 LibC: remove select() declaration
This already comes from sys/select.h
2023-06-02 17:29:09 +03:00
Bananymous 2d67a7153b LibC: rename [[noreturn]] to __attribute__((__noreturn__))
This compiles with C compiler
2023-06-02 17:28:36 +03:00
Bananymous 3fcc7c6768 LibC: stdlib.h doesn't seem to typedef wchar_t without __need_wchar_t 2023-06-02 17:27:31 +03:00
Bananymous 5d2a062b36 LibC: fix INFINITY definition typo 2023-06-02 17:27:14 +03:00
Bananymous e517ff6b6d LibC: fix DIR typedef for C code 2023-06-02 17:26:38 +03:00
Bananymous 7296846a81 LibC: complex.h undefs I before defining it 2023-06-02 17:13:09 +03:00
Bananymous 6abcb0de9b LibC: mbstate_t is empty struct 2023-06-02 17:10:29 +03:00
Bananymous 40f9d9d9bc LibC: fix sig_atomic_t definition 2023-06-02 17:08:43 +03:00
Bananymous 7aea8c45f7 Kernel: VFS now has max link depth of 100 2023-06-02 12:50:40 +03:00
Bananymous 7bbdee6cc4 Kernel: Symlinks are now working
We still have to implement loop or depth detection
2023-06-02 11:43:46 +03:00
Bananymous efb3271588 BAN: Fix function call in Vector 2023-06-01 00:50:04 +03:00
Bananymous ff548bd898 Kernel: Shell can now list symlinks 2023-06-01 00:25:53 +03:00
Bananymous 835d32814d Kernel: add basic support for symlinks 2023-06-01 00:24:45 +03:00
Bananymous 79d1f665f2 Kernel: Shell opens standard files 2023-05-31 23:14:15 +03:00
Bananymous 9c818d3da0 BuildSystem: cmake creates /usr/bin 2023-05-31 23:13:53 +03:00
Bananymous a378e59432 BuildSystem: link libraries when they change
This also fixed the need for manual linkin on firt build
2023-05-31 23:01:40 +03:00
Bananymous a75a3f7a5f Userspace: make test program link against libc on change 2023-05-31 22:36:47 +03:00
Bananymous 0f412e570c LibC: add execl 2023-05-31 22:36:26 +03:00
Bananymous 1daa1f5611 Kernel: Cleanup exec code 2023-05-31 22:36:05 +03:00
Bananymous 24a190d1f7 Kernel: Add SYS_EXEC syscall 2023-05-31 20:57:33 +03:00
Bananymous b48b239882 Kernel: Implement Process::exec() 2023-05-31 20:56:29 +03:00
Bananymous 5fb69300ca Kernel: Move userspace entry functions to Process instead of Thread 2023-05-31 19:31:10 +03:00
Bananymous dcb23f686f Kernel: boot.S maps GiB as single pdpte 2023-05-31 00:51:15 +03:00
Bananymous 650570e57d Kernel: Fix comment 2023-05-31 00:51:15 +03:00
Bananymous 1f5a36f074 Kernel: PageTable destructor works now
we are successfully booting higher half kernel now :)
2023-05-31 00:44:14 +03:00
Bananymous aeaf2cd3f1 Kernel: PageTable::create_userspace() now works 2023-05-31 00:44:14 +03:00
Bananymous 959c0fc572 Kernel: kmalloc free error prints the pointer 2023-05-31 00:34:56 +03:00
Bananymous e3d3788f28 Kernel: RSDP location is now done with virtual addresses 2023-05-31 00:34:21 +03:00
Bananymous 850b7f27a2 Kernel: Physical range now calculates RAM with physical addresses 2023-05-31 00:33:44 +03:00
Bananymous 0939f23b04 Kernel: Heap gets multiboot pointer with P2V 2023-05-30 23:57:44 +03:00
Bananymous 67886b0c5a Kernel: Move V2P and P2V to Memory/Types.h 2023-05-30 23:57:03 +03:00
Bananymous 56eb6fb4ce Kernel: PageTable::map_range_at maps correctly the last page 2023-05-30 23:56:07 +03:00
Bananymous 81cf389754 Kernel: Booting with higher half kernel gets to Heap initialization 2023-05-30 22:21:12 +03:00
Bananymous 0907965dc5 Kernel: start work on higher half kernel 2023-05-30 08:00:17 +03:00
Bananymous 79315d318c Toolchain: lib gcc is wuild with mcmodel=large 2023-05-30 07:59:41 +03:00
Bananymous e8f820ef8d LibC: Fix some headers to make gcc build again 2023-05-30 01:17:45 +03:00
Bananymous 210d90ec79 Kernel: We now identity map full GiB in boot.S
The paging structure is pre-built so no unnecessary calculations are done
2023-05-30 00:08:52 +03:00
Bananymous 8bea18a6f2 Kernel: Remove unused includes of CriticalScope 2023-05-29 21:15:55 +03:00
Bananymous 861966eed6 Kernel: Remove unnecessary usages of PageTableScope
This should be used as few times as possible since it calls 'cli'
2023-05-29 21:11:29 +03:00
Bananymous 5bb1f2a48c Kernel: Rename MMU to PageTable
This is more descriptive name for what it actually represents
2023-05-29 21:06:09 +03:00
Bananymous fb17af4844 Kernel/LibC: opening standard files is done in libc 2023-05-29 20:21:19 +03:00
Bananymous 9a8512887f LibC: open() now just returns syscall(SYS_OPEN, ...)
errno is handled in syscall()
2023-05-29 20:19:17 +03:00
Bananymous b8ec8918b7 Kernel: We can't lock the MMU lock in load()
It needs to be callable always by scheduler
2023-05-29 19:39:35 +03:00
Bananymous ff83f967d8 Kernel: Make RecursiveSpinLock thread safe
also SpinLock is now implemented with gcc builtins
2023-05-29 19:38:09 +03:00
Bananymous 998999a755 Kernel: Add SYS_SLEEP 2023-05-28 22:34:48 +03:00
Bananymous 09666adc53 Kernel: fork() now copies allocations through FixedWidthAllocator 2023-05-28 21:34:35 +03:00
Bananymous ec73db0057 Kernel: Move page macros to Types.h 2023-05-28 21:03:08 +03:00
Bananymous 833642d405 Kernel: ISR will now crash userspace process instead of panicing kernel 2023-05-28 20:53:10 +03:00
Bananymous f04399c3a0 Kernel: fork() now copies allocation done through GeneralAllocator 2023-05-28 20:37:39 +03:00
Bananymous f2d767b799 Kernel: Add bareboness fork() function 2023-05-28 18:08:49 +03:00
Bananymous 3e93dae53c Kernel: Add invalidate() to MMU 2023-05-28 18:05:49 +03:00
Bananymous 187bb046aa Kernel: fix MMU::map_page_at()
We used to only reassign if flags changed
2023-05-28 17:57:05 +03:00
Bananymous dd4973ac35 LibC: fputs uses fputc instead of putc 2023-05-28 17:48:34 +03:00
Bananymous b021d3eebd Kernel: Processes and Threads use VirtualRange memory allocations 2023-05-28 17:48:34 +03:00
Bananymous 15842db83e Kernel: Move PhysicalRange to its own file and add VirtualRange 2023-05-28 17:48:34 +03:00
Bananymous 869de7283f Kernel: MMU::get() is now MMU::kernel
MMU is can now be locked with RecursiveSpinLock.

Scheduler now has get_current_tid() that works before the Scheduler
is initialized. This allows RecursiveSpinLock usage early on.
2023-05-28 16:18:18 +03:00
Bananymous a2ee543fa1 Shell: we now link BAN (we can't use it though) 2023-05-26 22:31:21 +03:00
Bananymous eb24d32383 Kernel: Directory listing is working again 2023-05-26 22:31:21 +03:00
Bananymous 565e3db22d Kernel: kmalloc debug_dump is marked [[maybe_unused]] 2023-05-26 22:31:21 +03:00
Bananymous faf14b880e LibC: Rewrite all the headers.
We now have more or less posix issue 2018 conforming libc headers.

This was a really time consuming and boring operation but it had to
be done.

Now we get to actually start implementing libc :)
2023-05-26 22:31:21 +03:00
Bananymous faa7bc6043 BAN: libban is now build into library dir 2023-05-26 22:31:21 +03:00
Bananymous 03c64b950b BAN: Errors.h can be included from userspace 2023-05-26 22:31:21 +03:00
Bananymous 27147790fd Userspace: Start work on shell 2023-05-26 22:31:21 +03:00
Bananymous 1658e925f2 Kernel: Add bareboness possibility to set termios 2023-05-26 22:31:21 +03:00
Bananymous 82dcec9576 Buildsystem: Fix userspace link order 2023-05-26 22:31:21 +03:00
Bananymous 80d9f6131b Kernel/LibC: move file offset back to kernel
This makes keeping track of offsets easier and more proper
2023-05-26 22:31:21 +03:00
Bananymous e0a72defa2 Kernel: Add argc and argv to process entry 2023-05-16 00:27:49 +03:00
Bananymous e0a7e242f8 Kernel: Add MMUScope
This disables interrupts and loads specified mmu for the
scope it lives in
2023-05-16 00:26:39 +03:00
Bananymous 6acb86c14b Kernel: MMU keeps track of the current 2023-05-16 00:26:39 +03:00
Bananymous 8ec675cca6 LibC: fix bugs with printf 2023-05-15 22:47:08 +03:00
Bananymous 591d03de95 BAN: add is_arithmetic and is_signed to Traits.h 2023-05-15 20:26:29 +03:00
Bananymous ec316391c5 Kernel: argc is passed as zero to userspace 2023-05-11 18:28:32 +03:00
Bananymous 7543fadfa8 LibC: printf now prints 0 as integer 2023-05-11 18:20:37 +03:00
Bananymous bbaf1223f3 BuildSystem: add helper to create userspace programs 2023-05-11 18:10:06 +03:00
Bananymous 177b205c48 BuildSystem: userspace has cmake target 2023-05-11 16:19:53 +03:00
Bananymous 729ff267d7 LibC: add function declarations to sys/stat.h 2023-05-11 15:11:33 +03:00
Bananymous d9be14e1fb LibC: add function declarations to dirent.h 2023-05-11 01:42:52 +03:00
Bananymous 8a9f9b07e7 LibC: add definitions to math.h 2023-05-11 01:40:42 +03:00
Bananymous c989a01913 LibC: add defines in stdio.h 2023-05-11 01:39:16 +03:00
Bananymous 5188efcc57 LibC: add function declarations to unistd.h 2023-05-11 00:34:03 +03:00
Bananymous c05a5b796b LibC: add function declarations to string.h 2023-05-11 00:34:03 +03:00
Bananymous f0058e67c2 LibC: add function declarations to math.h 2023-05-11 00:34:03 +03:00
Bananymous 508d6311de LibC: define all errnos and strerror{name,desk}_np 2023-05-11 00:34:03 +03:00
Bananymous 7c6bf40d0d LibC: add function declarations to time.h 2023-05-11 00:34:03 +03:00
Bananymous a74343c589 LibC: add more types to sys/types.h 2023-05-11 00:33:53 +03:00
Bananymous d188576ef3 LibC: Add dummy signal.h 2023-05-10 23:20:27 +03:00
Bananymous d922c5e1d0 LibC: add toupper, tolower in ctype.h 2023-05-10 23:13:56 +03:00
Bananymous 0adf24fcad LibC: Add dummy setjmp.h 2023-05-10 23:00:53 +03:00
Bananymous 80e13965d9 LibC: Add dummy locale.h 2023-05-10 22:58:07 +03:00
Bananymous 36707ec87a LibC: implement printf conversions e, E, f, F 2023-05-10 22:36:03 +03:00
Bananymous 5f89f083a2 LibC: add math.h with floorl 2023-05-10 22:35:42 +03:00
Bananymous 0d9422ead8 BAN: add pow, log2, log10, log in math
These are implemented using x86 floating point assembly
2023-05-10 19:03:33 +03:00
Bananymous cae0a1cc60 LibC: add working f modifier to printf
This is implementation will write out of bounds if the conversion
takes more than 1024 characters (either super large number or very
big percision).

Also we dont handle NaN and infinity cases
2023-05-10 15:43:42 +03:00
Bananymous 480d92fce5 LibC: add better error string support 2023-05-10 02:22:31 +03:00
Bananymous 49fe3d0d4f LibC: add probably functional *printf
I wrote a general printf function that takes an putc function
pointer. We can use this to implement all the printf family
functions. I haven't done thorough testing with this, but it seems
to be functional for the most part
2023-05-10 02:00:28 +03:00
Bananymous ff2e2937a5 Kernel: Remove offset from OpenFileDescriptor
This is now handled on the libc side. There might be reasons to
have it in kernel side, but for simplicity's sake I'm moving it
to libc for now :)
2023-05-09 20:31:22 +03:00
Bananymous 0cc1fb53d5 LibC: Fix bug in *printf 2023-05-09 20:30:12 +03:00
Bananymous 512be884ed Kernel: Add barebones GeneralAllocator for >4096B 2023-05-08 22:10:49 +03:00
Bananymous f1667b398a Kernel: FixedWidthAllocator operates on MMU
Instead of Process* we use MMU& in FixedWidthAllocator since it is
everything it actually needs :)
2023-05-08 00:06:56 +03:00
Bananymous b0ec0f1a1a Kernel: We add FixedWidthAllocators on demand
On SYS_ALLOC we will add a new FixedWidthAllocator if the old ones
are already full or we don't have one with proper size. This allows
arbitary number of allocations as long as you have enough memory
available :)

Next I will be writing a general allocator for allocations larger
than 4096 bytes which should make SYS_ALLOC syscall complete :)
2023-05-07 23:57:01 +03:00
Bananymous 05046d6e93 BAN: Error uses 64 bit error codes 2023-05-07 02:09:52 +03:00
Bananymous 054c5450df LibC: syscall() now returns -1 on error and updates errno 2023-05-07 01:51:39 +03:00
Bananymous 12e42f40c5 Kernel/LibC: add free function for FixedWidthAllocator
I have to rework the syscall API and allocators in process. For
now this works well enough :)
2023-05-07 01:21:50 +03:00
Bananymous 890aa9aa15 BuildSystem: linker -O2 doesn't do anything? hopefully 2023-05-06 19:58:08 +03:00
Bananymous bcfd838131 Kernel: Add basic fixed width allocator for userspace
We have to move process stacks to the general heap and maybe map
kernel to higher half.
2023-05-06 19:58:08 +03:00
Bananymous 9c07add00f Kernel: Invalid physical addresses from heap are specified now by 0 2023-05-06 17:34:22 +03:00
Bananymous ab8aac7dcf Kernel: GDT tss selector is now 16 bit value 2023-05-06 17:34:22 +03:00
Bananymous f36b94d039 Kernel: MMU can now provide info about virtual address space 2023-05-06 17:34:22 +03:00
Bananymous 92daa831d1 Kernel: ISRs now print pid and tid 2023-05-06 00:10:15 +03:00
Bananymous e7a170c89f Kernel: 64 bit MMU now properly identity maps kernel 2023-05-05 14:19:28 +03:00
Bananymous 3c5d3eb8ad Kernel: TTY input process is now single instance
Process sends key events to the active (currently only) tty
2023-04-30 16:11:14 +03:00
Bananymous 55bb0084aa Kernel: namespace and function renames
MMU moved to namespace kernel
Kernel::Memory::Heap moved to just Kernel
MMU::map_{page,range} renamed to identity_map_{page,range}

Add MMU::get_page_flags
2023-04-28 14:48:38 +03:00
Bananymous acf5660090 Kernel: Add set_tss_stack() to 32 bit 2023-04-28 14:44:23 +03:00
Bananymous 9d7530662f Kernel: dprintln file name is now relative
This makes file names much shorter
2023-04-28 14:43:19 +03:00
Bananymous 9cf09165b5 BAN: Add is_power_of_two to Math functions 2023-04-28 14:42:49 +03:00
Bananymous e1a6e7c3ac LibC: add proper stdlib.h header
Function declarations taken from the posix specifications
2023-04-27 15:14:03 +03:00
Bananymous 3537d53d5c LibC: add link to posix stdio.h 2023-04-27 14:16:25 +03:00
Bananymous c20ba3064d Userspace: Simple stdio test 2023-04-25 14:50:26 +03:00
Bananymous a22caa38d2 Kernel: Scheduler updates tss stack on thread execution 2023-04-25 14:49:50 +03:00
Bananymous dc0f8b383f Kernel: Usespace threads now have an interrupt stack 2023-04-25 14:49:18 +03:00
Bananymous f1a0625b61 Kernel: TSS stack can be set after initialization 2023-04-25 14:48:12 +03:00
Bananymous 02b961fd7e Kernel: Stack pointer out of bounds now panics with a message 2023-04-25 13:40:55 +03:00
Bananymous 1d42b26fce LibC: Fix some bugs 2023-04-25 13:27:01 +03:00
Bananymous 6e01e04922 Kernel: Fix bug in Process::create_userspace()
We used to write more than we needed to. This could lead to unwanted
page faults
2023-04-25 13:21:28 +03:00
Bananymous 79812b34b0 LibC: fread() now does a single syscall 2023-04-25 12:38:08 +03:00
Bananymous b7c2ea8d46 Kernel: Fix possible dead lock in Process::read() 2023-04-23 14:46:18 +03:00
Bananymous cd74b2167d LibC: Write mostly functioning stdio 2023-04-23 14:32:37 +03:00
Bananymous 9b2a577fc3 Kernel: Add ASSERTion to Process::exit()
This is just so I don't forget that exit can currently only
be called on the executing thread itself
2023-04-22 19:05:27 +03:00
Bananymous 2dd09163e6 Kernel: Process can now load 32 bit elf files on i386
We are page faulting on process exit but I'm investigating
the reason.
2023-04-22 19:03:30 +03:00
Bananymous 850ff93940 LibELF: Add 32 bit support 2023-04-22 19:00:18 +03:00
Bananymous fb6add2b4a Kernel: Fix 32 bit MMU 2023-04-22 18:22:39 +03:00
Bananymous 48445f12ac Kernel: More proper paging in Elf loading 2023-04-22 18:17:44 +03:00
Bananymous 792fad2a03 Kernel: d{print,warn,error}ln(...) now has a spinlock 2023-04-22 17:58:51 +03:00
Bananymous cc04990ce3 Kernel: Process now frees up its pages on destruction 2023-04-22 16:54:46 +03:00
Bananymous 7530482cc2 Kernel: Cleanup process creation for userspace 2023-04-22 16:51:50 +03:00
Bananymous 33d8c518e9 Kernel: Fix some deadlocks in the Process 2023-04-22 16:19:57 +03:00
Bananymous e0ce2394fe Kernel: Remove obsolete userspace stuff from kernel 2023-04-22 15:38:45 +03:00
Bananymous fadce063a7 Kernel: Usespace programs are now ran through ELF files
only 64 bit elf files are supported for now.
2023-04-22 15:35:32 +03:00
Bananymous ef0263e32d LibELF: Header printing can now be turned off 2023-04-22 15:34:09 +03:00
Bananymous 4588e25d27 LibELF: ELF now has methods for accessing more attributes
You can now access the program headers and the memory itself
2023-04-22 15:31:05 +03:00
Bananymous ff8c0086e2 LibC: puts() now just calls syscall(SYS_WRITE, ...) 2023-04-22 15:29:38 +03:00
Bananymous dc1b7cf08f LibC: syscalls have now proper argument order 2023-04-22 15:29:15 +03:00
Bananymous 26fe6ad898 Kernel: kmalloc does not dump stack trace
dump_stack_trace() page faults and i dont feel like debugging this
now :)
2023-04-21 13:45:13 +03:00
Bananymous fc71d2f7c4 Kernel: Don't call 'cli' on interrupt
This is automatically done because we are using interrupt gates
in the IDT
2023-04-21 11:18:08 +03:00
Bananymous 35e949ef5e Kernel: Fix syscall return value in 32 bit 2023-04-21 11:08:02 +03:00
Bananymous 9c506ef85b Kernel: Stack pointer is validated when updated
This allows us not to fail stack pointer when in syscall since
interrupts use their own stack
2023-04-21 10:40:24 +03:00
Bananymous b1c7af38d0 Kernel: Add barebones per process virtual addresses
We now assign every (userspace) process its own MMU which we load
in scheduler. This allows every process to have separate virtual
address space.

This is very hackish implementations but it works for now
2023-04-20 00:45:41 +03:00
Bananymous ea0c9b639f Kernel: Add some bareboness functionality to map virtual addresses 2023-04-19 23:51:36 +03:00
Bananymous 0030f035be Kernel: Rename MMU::{un,}allocate... to MMU::{un,}map
This is more appropriate name for the behaviour :D
2023-04-19 21:50:30 +03:00
Bananymous c26529ae86 Kernel: kmalloc will always print debug on failed alloc 2023-04-19 18:05:01 +03:00
Bananymous 7d57d2fcfb LibELF: remove unused file 2023-04-19 17:32:12 +03:00
Bananymous e8a0df54b1 Kernel: ATAController will fail to initialize in native mode 2023-04-19 17:29:36 +03:00
Bananymous 33393335c8 Kernel: PCI devices now report their prog_if 2023-04-19 16:43:05 +03:00
Bananymous c5b02bb9f5 Kernel: Shell fix file reading
We were reading 8 bytes at a time instead of the 1024 we were
supposed to :DD
2023-04-19 14:09:23 +03:00
Bananymous a1047918d2 Kernel: Shell now has exit() command 2023-04-19 12:53:09 +03:00
Bananymous fd81e31050 Kernel: Fix Process::exit() and where it is called from
cksum doesn't seem to work anymore on big files. I have to look
into this. It locks up after couple of seconds...
2023-04-19 12:52:21 +03:00
Bananymous 160315c4d0 Update README.md
Add cool badges :D
2023-04-19 00:46:42 +03:00
Bananymous d9b7747fc5 LibC: exit() calls _fini() 2023-04-19 00:42:00 +03:00
Bananymous dcce18799f Kernel: We now launch Shell again on boot
Adding this just before push :D
2023-04-19 00:41:24 +03:00
Bananymous a9a15ea2c0 Kernel: init2 is now launched as a process instead of thread
Also only process can now add threads to scheduler. Nobody should
have raw access to scheduler and everything should be through
Process::current() or irqs (reschedules)
2023-04-19 00:39:06 +03:00
Bananymous d63716db96 Kernel: Process is not reference counted any more
This was not necessary and it made things needlessly complicated
2023-04-19 00:34:18 +03:00
Bananymous 3ca623349a BAN: Add {TRY,MUST}_REF for references
It is annoying that we have to have separate macros for these but
I can't find a way to cleanly return lvalue reference from statement
expression. Currently we cast the reference to pointer and return
unreference the pointer outside of the expression.

This feature will probably not be used any time soon, but atleas
it is implemented if I need it one day
2023-04-19 00:31:15 +03:00
Bananymous 121f4bc1dd BAN: TRY and MUST macros use rvalue references 2023-04-19 00:11:15 +03:00
Bananymous 8e31ef9905 BAN: Add specialization for ErrorOr<LValueReference>
ErrorOr can now return a reference :)
2023-04-18 22:02:47 +03:00
Bananymous 2c52e0aad8 BAN: Variant with reference now supports copy/assign 2023-04-18 20:21:23 +03:00
Bananymous 06916f56be BAN: Variant now works with references
References can be assigned with the set() method. Construction nor
assigment operators cannot be used with references to avoid ambiguity
with what assignment to reference does.

You can set the underlying reference with the set() method and access
it with the get() method.

The references are stored as pointers to the object under the hood
which means that size of a reference is sizeof pointer.
2023-04-18 19:53:34 +03:00
Bananymous c3df0bd15e BAN: Variant::set now copy/move assigns when possible 2023-04-18 19:10:22 +03:00
Bananymous b41a8e2829 BAN: Variant now has variadic template types 2023-04-18 18:29:48 +03:00
Bananymous 96ac072166 Kernel: Heap allows us to take free pages.
The API is kinda weird and will probably be reworked in near future
but this will work for now :)
2023-04-18 10:18:15 +03:00
Bananymous 295a27f16a Kernel: Panic wont print stacktrace if it has already paniced
This prevents stack trace dump to panic and loop
2023-04-18 10:18:15 +03:00
Bananymous 88f60b5e41 Kernel: Map all ACPI tables on initialization
This makes their usage much easier and less error prone

They won't mapped be processes when we get to there, so this won't be
a problem
2023-04-18 10:18:15 +03:00
Bananymous 1c22e90fa0 Scripts: remove disk build from install-usb.sh
You can still use the install-usb.sh script from the build directory.
2023-04-18 10:18:15 +03:00
Bananymous d23604b0d5 Kernel: Handle some Heap edge cases 2023-04-18 10:18:15 +03:00
Bananymous c5347e6707 Kernel: Reorder boot initialization
We now create the TTY as soon as possible so we can show console
output without serial port.
2023-04-18 10:18:15 +03:00
Bananymous bf0c7b9ae8 Kernel: Heap implementation can now give free pages from all of RAM 2023-04-18 10:18:15 +03:00
Bananymous 0374b68fa1 Kernel: Remove unused file 2023-04-18 10:18:15 +03:00
Bananymous 633929629c Kernel: Start working on heap 2023-04-18 10:18:15 +03:00
Bananymous 6a3b3213cf Kernel: Kmalloc now has its memory statically allocated
We don't use the memory map given by bootloader since this automatically
maps the memory to a available space.
2023-04-18 10:18:15 +03:00
Bananymous fdb4eb6042 Kernel: Move kmalloc and MMU to Memory directory 2023-04-18 10:18:15 +03:00
Bananymous 998ae511a3 LibELF: Start implementing elf library 2023-04-18 10:18:15 +03:00
Bananymous c897b90c28 Create LICENCE 2023-04-13 00:38:24 +03:00
Bananymous 3f9d6f0311 LibC: add needed stubs to build executables with our compiler 2023-04-12 17:53:02 +03:00
Bananymous 8ee63f8264 Kernel: We can create basic userspace processes
These are still allocated on the kernel memory
2023-04-12 17:52:36 +03:00
Bananymous 34358b8471 Kernel: Scheduler can now terminate processes threads 2023-04-12 17:49:04 +03:00
Bananymous 5b1af4ec47 Kernel: Debug::dump_stack_trace now 'detects' repeating function 2023-04-12 01:32:41 +03:00
Bananymous 071d7af58a Kernel: kmalloc does not check for corruptiong unless it cannot allocate
We 'don't care' if kmalloc is corrupted unless it prevents us from
allocating memory. Scheduler should be catching stack overflows either
way and is much more efficient
2023-04-12 00:24:02 +03:00
Bananymous 3201c3654e Kernel: Threads now use only 4 KiB stack :) 2023-04-12 00:22:08 +03:00
Bananymous 6ed3023725 Kernel: Fix TTY spinlock usage 2023-04-12 00:20:04 +03:00
Bananymous 892a63aec5 Kernel: Shell TTY_PRINT is now function instead of macro
This makes functions uses way less stack
2023-04-12 00:18:58 +03:00
Bananymous 3fe889d4a4 BuildSystem: GCC will now complain on functions with 1 KiB stack 2023-04-12 00:18:06 +03:00
Bananymous c35e7368f2 BAN: Variant now aligns its data properly 2023-04-12 00:17:45 +03:00
Bananymous cafd546ce8 Kernel: kmalloc now detects corruption
Kmalloc checks if its nodes have corrupted. This was happening
because of stack overflow.
2023-04-11 23:36:46 +03:00
Bananymous 5810a77cbf Kernel: Mark Scheduler::start() as noreturn as appropriate 2023-04-11 23:33:20 +03:00
Bananymous 6f7045ead2 Kernel: kernel doesn't allocate large blocks of data on stack
We used to allocate 1 KiB blocks on multiple places on stack. This
is a problem, since kernel stack shouldn't have to be too big
2023-04-11 23:31:58 +03:00
Bananymous 40f9a42c00 Kernel: Scheduler will panic if it encounters stack overflow 2023-04-11 23:29:21 +03:00
Bananymous 9abe1f27bb Kernel: Process now locks the mutex while modifying open files
This allows processes to be actually removed from the list instead
of dead locking
2023-04-11 23:28:16 +03:00
Bananymous aa2aee684b Kernel: remove message from BAN::Error
We don't store the error message anymore in BAN::Error.
Instead we store a error code that can be mapped into a string.
This allows BAN::Error to only take 4 bytes instead of 128.

We should also make some kernel initialization just panic instead
of returning errors since they are required for succesfull boot
anyway.
2023-04-11 23:25:21 +03:00
Bananymous cfa025acae BAN: Move RefPtr to its own file and create New.h
New.h contains definitions for placement new operators and
BAN::allocator and BAN::dealloctor
2023-04-10 21:07:25 +03:00
Bananymous 781cc78a1f BuildSystem: Fix header copying to sysroot
We used to copy all headers everytime to sysroot which caused
rebuild of the whole os. Now we use the cmake command
'copy_directory_if_different' which seemed to fix this issue :)
2023-04-10 21:07:25 +03:00
Bananymous f924ac9265 Kernel: Threads can now be terminated mid execution 2023-04-10 21:07:25 +03:00
Bananymous df6e8a6562 Kernel: Add small wait in ATA driver before reading/writing
This allows bochs to boot again
2023-04-10 21:07:25 +03:00
Bananymous 3f01f936a1 Kernel: Fix framepointers on started threads 2023-04-10 21:07:25 +03:00
Bananymous 25ddc24754 BuildSystem: cmake can now build out libc
I can't seem to get libc++ build to work...
2023-04-10 21:07:25 +03:00
Bananymous 989f9ec5fe LibC: remove old unused files 2023-04-07 02:26:44 +03:00
Bananymous 3fbee2c835 BuildSystem: remove now obsolete include directories from kernel build 2023-04-07 02:25:47 +03:00
Bananymous b10b3cbe3b Add a screenshot to README 2023-04-06 21:01:27 +03:00
Bananymous 77e94e1d3b General: Write basic README 2023-04-06 20:59:45 +03:00
Bananymous 4a3bfaff90 Scripts: linecount does not count lines from toolchain/ 2023-04-06 20:31:10 +03:00
Bananymous 2a8dc58b6a BuildSystem: Cleanup cmake code
The buildsystem is kind of a mess. I will be writingn build
instructions soon.
2023-04-06 20:31:10 +03:00
Bananymous 8a8793fd2d BuildSystem: you can now build the toolchain with cmake 2023-04-06 00:23:02 +03:00
Bananymous 7ab9e420ac Update .gitignore 2023-04-06 00:02:47 +03:00
Bananymous 43ca62de47 BuildSystem: Create script for os specific toolchain 2023-04-06 00:02:13 +03:00
Bananymous a5830c5424 LibC: add stubs for a lot of functions 2023-04-05 23:58:40 +03:00
Bananymous b6896a6d85 LibC: sys/types uses 'typedef' instead of 'using' 2023-04-05 15:03:24 +03:00
Bananymous 9e0b4132ee Kernel: Shell now somewhat functions again
I will not be fixing the shell implementation until I get to
userspace
2023-04-05 11:37:41 +03:00
Bananymous 68158324f4 Kernel: Fix TTY echo and canonical flag behaviour 2023-04-05 11:35:19 +03:00
Bananymous a420be6b20 Kernel: Fix ansi control sequence cursor movement 2023-04-05 03:07:52 +03:00
Bananymous f281543255 Kernel: Add tty to process and make termios modifiable 2023-04-05 02:53:28 +03:00
Bananymous 67ff01e915 LibC: Add errno ENOTTY 2023-04-05 02:47:37 +03:00
Bananymous dcf0969e2d Kernel: TTY now supports clearing 2023-04-05 02:04:18 +03:00
Bananymous 4f522d337a Kernel: Add partial support for shell
We don't handle arrow keys, and the tty does not know how to clear
the screeen :D
2023-04-05 01:30:58 +03:00
Bananymous af0979ec32 Kernel: Process can now initialize stdio
This allows the use of the fds STD{IN,OUT,ERR}_FILENO
2023-04-05 01:10:25 +03:00
Bananymous 63e863ad35 LibC: Add unistd.h with STD{IN,OUT,ERR}_FILENO definitions 2023-04-05 00:59:48 +03:00
Bananymous 7f95444bb5 Kernel: Start work on making tty a file
TTY is now a file that you can read from/write to. I still have
to port shell to use this new interface
2023-04-05 00:56:09 +03:00
Bananymous 783842bac2 Kernel: add virtual write function to inode 2023-04-03 20:29:07 +03:00
Bananymous e1534ad25b Kernel: Fix keys in PS2Keymap 2023-04-03 20:25:23 +03:00
Bananymous ee1f3623ce Kernel: Fix typo 2023-04-03 19:56:55 +03:00
Bananymous dffdb51713 BuildSystem: remove non-existent file from kernel CMakeLists.txt 2023-04-03 19:02:25 +03:00
Bananymous daa35aaf7d BAN: Add data() member function to Array 2023-04-03 17:00:52 +03:00
Bananymous b87dae7e7c Kernel: Device dev and rdev number is done more properly
Also hd* partitions are now 1 indexed instead of 0
2023-04-03 11:43:16 +03:00
Bananymous 86d777e2eb LibC: add device macros in sys/sysmacros.h 2023-04-03 10:59:15 +03:00
Bananymous 2ffee63889 Kernel: Cleaner partition parsing errors 2023-04-03 09:55:49 +03:00
Bananymous c936aac777 Kernel: Optimize scheduler idling
Now after each interrupt we will ask the scheduler to reschedule
if the current thread is the idle thread. This allows semaphore
unblocking to be practically instant when there is only one thread
executing.

Now disk reading is back to ~3 MB/s for single threaded process
2023-04-03 01:51:05 +03:00
Bananymous 46d65471d9 Kernel: ATA now uses irqs instead of polling
Reading is now much slower at ~500 kB/s it was around 3 MB/s.
This is probably mostly due semaphore blocking taking atleast
until next reschedule (1 ms itervals). This will be a problem
as long as we are using only single processor.

I could try to use {READ/WRITE}_MULTIPLE commands, but since
most of the disk reads are 2 sectors (inode block size) this
will at most double the speed.

Most efficient speed up would of course be caching disk access
data and inodes overall.
2023-04-03 00:03:38 +03:00
Bananymous dcc174b62e Kernel: Move ATADevice to its own file from ATAController
The API is kinda weird since device reads/writes go from
ATADevice -> ATAController -> ATADevice
but this is for now atleast necessary since ATAController has(?)
to keep all devices from using the disks at the same time
2023-04-02 18:26:19 +03:00
Bananymous bdc7a99c59 Kernel: ATA controller waits now before read/write until disk is ready
Qemu used to freeze on disk writes but now it seems fine
2023-04-02 05:37:17 +03:00
Bananymous 2aad357c18 BuildSystem: building for 32-bit works now 2023-04-02 05:03:17 +03:00
Bananymous ccf51cec5c Scipts: linecount doesn't count lines in build/ 2023-04-02 04:09:54 +03:00
Bananymous 4c5176f751 BuildSystem: We are now using cmake instead of plain make
I have been annoyed for a while since I had to build everything
when running the os since the buildsystem was really bad.

I wanted to rewrite the whole build system and changed to using cmake

:)
2023-04-02 04:07:27 +03:00
Bananymous d71f1f24e4 LibC: Combine string.h functions definitions to single file 2023-04-02 00:00:29 +03:00
Bananymous cbb0f6be9a Kernel: cleanup includes
I went quickly went through all files since I found some weird
includes :D
2023-04-01 02:14:49 +03:00
Bananymous a60f8098ee Kernel: Fix traversing back from mount points 2023-04-01 01:54:35 +03:00
Bananymous 8bffbf64b9 Kernel: Add inodes '.' and '..' to device manager 2023-04-01 01:33:04 +03:00
Bananymous d91a5bccf4 Kernel: Reads return 0 bytes read at the end of device
We used to not have any idea if we where already at the end of device.
Also fixed couple of copy-paste errors from read->write
2023-04-01 00:55:07 +03:00
Bananymous 1ca0d3a3c1 Kernel: Shell cleanup cat command 2023-04-01 00:54:39 +03:00
Bananymous 653f688567 Kernel: Shell 'time' prints the time even if command fails 2023-04-01 00:30:33 +03:00
Bananymous 8b5d8d9f8a Kernel: Process gets absolute paths for mount 2023-04-01 00:30:11 +03:00
Bananymous c45ada6ccb Kernel: fix ext2 failed creation memory leak 2023-04-01 00:22:03 +03:00
Bananymous db65cfeb8a Kernel: Cleanup ATA device initialization 2023-03-31 00:58:57 +03:00
Bananymous a3ba6da6f3 Kernel: Move DeviceManager to its own file 2023-03-30 22:39:45 +03:00
Bananymous b048630e5b Kernel: Improve locking in Process, VFS and ATAController
We used to block on all process access. This meant that shell
reading the keyboard input would block all VFS access making disk
accesses practically impossible. We now block only when it is
necessary :)
2023-03-30 22:02:16 +03:00
Bananymous dcee92a6bc Kernel: Thread is no longer RefCounted
This makes developement with Scheduler much easier against compiler
optimizations. I could now remove the pragma GCC optimize stuff.
2023-03-30 19:16:51 +03:00
Bananymous c2e3b422cc Kernel: Add Semaphore to block threads 2023-03-30 18:46:33 +03:00
Bananymous 7b7f4eb141 BAN: LinkedList::remove now returns iterator to the element after 2023-03-30 18:46:19 +03:00
Bananymous d4c03d3939 Kernel: root partition is now passed from the commandline 2023-03-30 18:46:19 +03:00
Bananymous 5d459130a7 BAN: StringView::split(char, bool) is now thread safe
I had to duplicate some code, but this is better since I would like
to not use BAN::Function for something like this
2023-03-30 16:35:38 +03:00
Bananymous 8e68d2e3ea Kernel: Shell can now mount partitions 2023-03-30 15:06:41 +03:00
Bananymous 30c33b55e3 Kernel: Inode::Mode is now a struct so we can have functions in it 2023-03-30 14:41:15 +03:00
Bananymous e2791e5260 Kernel: StorageDevices and Controllers are now devices 2023-03-30 14:22:15 +03:00
Bananymous 88a8bd659d Kernel: Add IFBLK, IFLNK, IFSOCK to Inode::Mode 2023-03-30 13:15:46 +03:00
Bananymous f9cc114907 Kernel: All devices have atime, mtime, ctime at their creation 2023-03-30 13:15:46 +03:00
Bananymous 9a4d603a62 Kernel: Remove the mount test from VFS 2023-03-30 11:43:24 +03:00
Bananymous 78ea4b2207 Kernel: fix stat command and device numbers 2023-03-30 10:43:08 +03:00
Bananymous ab3cdea548 Kernel: Rewrite mounting code 2023-03-29 21:34:48 +03:00
Bananymous 10e0c90fde BAN: String add front() and back() helpers 2023-03-29 14:10:29 +03:00
Bananymous dd84a2175f Kernel: Move Partition out of StorageDevice and rename functions 2023-03-29 13:23:01 +03:00
Bananymous ae05ad3f38 Kernel: Shell commands 'ls' and 'stat' recognize character devices 2023-03-29 11:56:33 +03:00
Bananymous 62fb233eb1 Kernel: DeviceManager is now a 'FileSystem' so it can expose devices
Shell reads keyboard through /dev/input :)
2023-03-29 11:50:46 +03:00
Bananymous cd0d10b64e LibC: add ENOBUFS errno 2023-03-29 10:58:25 +03:00
Bananymous 426b1482dd Kernel: Fix keymap numlock behaviour 2023-03-29 03:18:22 +03:00
Bananymous 16d9d29971 Kernel: Remove obsolete KeyboardLayout/
This was used by the old input system. Currently keyboard layout is
hardcoded to finnish. But it will be reworked as something read from
the filesystem
2023-03-29 03:09:14 +03:00
Bananymous a4fb805315 Kernel: Add linecount.sh script
This calculates the number of lines of code in the whole project :D
2023-03-29 03:06:57 +03:00
Bananymous fa8e921ee8 Kernel: Rework the whole input system
We now use Device abstraction that will allow us to provide devices
to userspace through /dev. Currently Shell reads from first and only
device (it being PS/2 Keyboard).
2023-03-29 03:05:16 +03:00
Bananymous e5c3486826 BAN: Vector now takes optional argument for default value on resize 2023-03-28 23:10:36 +03:00
Bananymous 8e626b8d1f BAN: Implement basic Circular Queue 2023-03-28 21:44:02 +03:00
Bananymous ea900ad744 Kernel: ACPI unmap_header does not do anything
We have to work with MMU mapping/unmapping to be user friendly
2023-03-28 02:56:44 +03:00
Bananymous 6a6717cdd3 BAN: ScopeGuard can now be disabled (it wont call the function) 2023-03-28 01:15:13 +03:00
Bananymous c7286396d8 Kernel: Move ACPI to its own file 2023-03-27 17:30:45 +03:00
Bananymous 0cebf248a3 BAN: move placement new declaration to Move.h 2023-03-27 03:38:06 +03:00
Bananymous 686b425eb9 Build System: Create base directory for the FS 2023-03-27 01:11:17 +03:00
Bananymous 666051fd34 Kernel/BAN: move unix time conversion to BAN and add stat to Shell 2023-03-27 00:49:58 +03:00
Bananymous e55860eb6b Kernel: Rework processes and VFS so we don't expose inodes
Everything is now done through a file descriptor.
2023-03-26 04:30:57 +03:00
Bananymous 32c3aca52f LibC: add dirent.h 2023-03-25 02:08:33 +02:00
Bananymous 9fd17ef73c LibC: Add stat structure 2023-03-24 18:08:22 +02:00
Bananymous 1fade1aa9e Kernel: Process::working_directory() is now thread safe
I realized you cannot return a stirng view and it to be thread safe
2023-03-24 01:46:25 +02:00
Bananymous 814f0b215d Kernel: Create RecursiveSpinLock and add it to Process
We now lock every function within Proccess, just to be sure.
Recursive lock allows us to use lock from the same thread even if
we already have the spinlock locked
2023-03-24 01:32:35 +02:00
Bananymous 5fd26b4ea8 Kernel: Lock process functions instead of the ata controller
Process has to use locks at least on some functions so multithreaded
disk io is safe. This seemed to fix corrupted reads from disk
2023-03-24 01:17:39 +02:00
Bananymous e5eab8bae4 Kernel: Ext2FS now does allocations better
We only have to allocate at the beginning of the all functions and
can properly exit before any disk reads if we run out of memory.

This makes development little bit 'harder' since the {read,write}_block
user must allocate a buffer of atleast block_size bytes.

I also made disk access to cause kernel panic on error since the error
handling during file write is something I don't want to think now.
The filesystem can easily corrupt so, I feel like when disk io starts
to fail I'll come back to this.
2023-03-23 23:22:31 +02:00
Bananymous 75c4f35e85 Kernel: Add possibiliity to create empty files on Ext2
Big rewrite for Ext2 for more easy and optimized code
2023-03-23 22:26:06 +02:00
Bananymous 1be8b2f514 LibC: add more typedefs to sys/types 2023-03-23 19:24:12 +02:00
Bananymous 633414bd20 Kernel: Remove block group descriptor cache from ext2fs
This will make improving the fs easier since we need to only update
the values on the disk
2023-03-23 18:52:58 +02:00
Bananymous 7ca6cd61be Kernel: Move get_unix_time to RTC namespace 2023-03-23 18:14:51 +02:00
Bananymous d6e4430692 LibC: add errno NAMETOOLONG 2023-03-23 14:48:42 +02:00
Bananymous efe73caf1b BAN: char* is now formatted as string and not pointer 2023-03-23 14:29:35 +02:00
Bananymous 3ef72e8a7b BAN: Modify Span constructors to keep constness correctly 2023-03-23 14:26:03 +02:00
Bananymous e9da63ad79 BAN: Add is_const to traits 2023-03-23 13:28:57 +02:00
Bananymous 9b56801c3d Kernel: StorageDevice and Ext2 "support" writing 2023-03-23 13:04:13 +02:00
Bananymous 5f6c58ffd2 Kernel: Shell now prints unix time with 'date' command 2023-03-23 11:13:51 +02:00
Bananymous 74949401bd Kernel: Cleanup GPT parsing code 2023-03-23 11:13:14 +02:00
Bananymous 4ffc69a6e4 Kernel: TTY buffer is resized on font size change
Shell also has better line wrapping. You still can't visually go
back to previous line, but atleas we now write from the beginning
of the line
2023-03-22 02:09:22 +02:00
Bananymous 61ac9833be Kernel: Reading from fd verifies that file is opened for reading 2023-03-22 01:55:58 +02:00
Bananymous dbf7d9f19e Kernel: Ext2 directory functions now fail on invalid blocks
Invalid blocks should only happen while writing to a file and
I think in that case we should just bail out instead of giving
you incomlete inode list or search result.
2023-03-22 01:55:57 +02:00
Bananymous a6e5a0b704 Kernel: cksum uses now a different crc32_table to match linux 'cksum' 2023-03-22 01:55:21 +02:00
Bananymous 337463ec16 Kernel: Ext2 can now read from non-block-size aligned offsets 2023-03-21 19:19:17 +02:00
Bananymous 072954480d Kernel: Remove obsolete Ext2FS::ext2_root_inode()
This was not used by anyone and the cast was wrong anyway
2023-03-21 18:19:48 +02:00
Bananymous 3652d11059 Kernel: Remove for_each_block from Ext2 2023-03-21 18:14:02 +02:00
Bananymous 54824aec74 Kernel: Cleanup font parsing
We use now the LittleEndian<> wrapper for PSF2 header and no more
magic constants in code
2023-03-20 19:48:08 +02:00
Bananymous a61cf61fd1 BAN: Add wrappers for little/big endian numbers 2023-03-20 19:48:08 +02:00
Bananymous 75e85def83 Kernel: prefs font does not allocate extra buffer 2023-03-20 19:48:01 +02:00
Bananymous f9ae1f0023 Kernel: Fix PC Screen font parsing
I had misread the format and the parsing code was incorrect. I also
changed fonts to store unicode codepoints as 32 bit integers, so
every character can be represented
2023-03-20 14:52:42 +02:00
Bananymous 896b4c280c Kernel: Font parsing uses Spans now 2023-03-20 13:35:54 +02:00
Bananymous ce3f268075 BAN: Implement basic Span
This is wrapper over contiguous block of memory e.g. Vector
2023-03-20 13:34:26 +02:00
Bananymous 8edabaea55 BAN: Add iterators to all containers with contiguous memory 2023-03-20 13:26:42 +02:00
Bananymous ec22b86e00 BAN: Add implementation for basic iterator for contiguous memory 2023-03-20 13:15:38 +02:00
Bananymous e4bcd98904 Kernel: Add basic mounting to VFS. 2023-03-19 05:51:25 +02:00
Bananymous cf2be54e8f LibC: add errno ENOTEMPTY 2023-03-19 05:43:40 +02:00
Bananymous 3ab62e83d3 LibC: add errno EEXISTS 2023-03-19 04:17:39 +02:00
Bananymous 3570764448 Kernel: Add comparison operator for inodes 2023-03-19 03:34:23 +02:00
Bananymous 6188023fb5 Create README.md 2023-03-18 04:05:59 +02:00
927 changed files with 92896 additions and 9742 deletions

13
.clangd Normal file
View File

@ -0,0 +1,13 @@
Diagnostics:
Suppress: target_unsupported_type
CompileFlags:
Remove: [
-fstrict-volatile-bitfields,
-fno-tree-loop-distribute-patterns
]
Add: [
-D__banan_os__,
-D__arch__=x86_64,
-D__x86_64__
]

8
.gitignore vendored
View File

@ -1,7 +1,5 @@
*.img
isodir
sysroot
.vscode/
.idea/
bochsrc
bx_enh_dbg.ini
build/
base/
script/fakeroot-context

0
.gitmodules vendored Normal file
View File

8
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,8 @@
# .pre-commit-config.yaml
exclude: '.patch$'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0 # this is optional, use `pre-commit autoupdate` to get the latest rev!
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace

38
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,38 @@
{
"configurations": [
{
"name": "banan-os",
"includePath": [
"${workspaceFolder}/BAN/include",
"${workspaceFolder}/kernel/include",
"${workspaceFolder}/userspace/libraries/*/include"
],
"defines": [
"__arch=x86_64",
"__enable_sse=1"
],
"compilerPath": "${workspaceFolder}/toolchain/local/bin/x86_64-banan_os-gcc",
"cStandard": "c17",
"cppStandard": "gnu++20",
"intelliSenseMode": "linux-gcc-x64"
},
{
"name": "banan-os-kernel",
"includePath": [
"${workspaceFolder}/BAN/include",
"${workspaceFolder}/kernel/include",
"${workspaceFolder}/userspace/libraries/*/include"
],
"defines": [
"__arch=x86_64",
"__is_kernel",
"__enable_sse=1"
],
"compilerPath": "${workspaceFolder}/toolchain/local/bin/x86_64-banan_os-gcc",
"cStandard": "c17",
"cppStandard": "gnu++20",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

10
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,10 @@
{
"cmake.configureOnOpen": false,
"editor.tabSize": 4,
"editor.insertSpaces": false,
"editor.detectIndentation": false,
"clangd.arguments": [
"--compile-commands-dir=${workspaceFolder}/build",
"-header-insertion=never"
]
}

3
BAN/.gitignore vendored
View File

@ -1,3 +0,0 @@
*.a
*.d
*.o

22
BAN/BAN/Assert.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <BAN/Assert.h>
#if __is_kernel
#include <kernel/Panic.h>
[[noreturn]] void __ban_assertion_failed(const char* location, const char* msg)
{
Kernel::panic_impl(location, msg);
}
#else
#include <BAN/Debug.h>
[[noreturn]] void __ban_assertion_failed(const char* location, const char* msg)
{
derrorln("{}: {}", location, msg);
__builtin_trap();
}
#endif

View File

@ -1,9 +0,0 @@
#include <BAN/Memory.h>
void* operator new(size_t size) { return BAN::allocator(size); }
void* operator new[](size_t size) { return BAN::allocator(size); }
void operator delete(void* addr) { BAN::deallocator(addr); }
void operator delete[](void* addr) { BAN::deallocator(addr); }
void operator delete(void* addr, size_t) { BAN::deallocator(addr); }
void operator delete[](void* addr, size_t) { BAN::deallocator(addr); }

9
BAN/BAN/New.cpp Normal file
View File

@ -0,0 +1,9 @@
#include <BAN/New.h>
void* operator new(size_t size) { return BAN::allocator(size); }
void* operator new[](size_t size) { return BAN::allocator(size); }
void operator delete(void* addr) { BAN::deallocator(addr); }
void operator delete[](void* addr) { BAN::deallocator(addr); }
void operator delete(void* addr, size_t) { BAN::deallocator(addr); }
void operator delete[](void* addr, size_t) { BAN::deallocator(addr); }

View File

@ -1,262 +0,0 @@
#include <BAN/Errors.h>
#include <BAN/Math.h>
#include <BAN/Memory.h>
#include <BAN/Move.h>
#include <BAN/String.h>
#include <BAN/StringView.h>
#include <string.h>
namespace BAN
{
String::String()
{
MUST(copy_impl(""sv));
}
String::String(const String& other)
{
MUST(copy_impl(other.sv()));
}
String::String(String&& other)
{
move_impl(move(other));
}
String::String(StringView other)
{
MUST(copy_impl(other));
}
String::~String()
{
BAN::deallocator(m_data);
}
String& String::operator=(const String& other)
{
MUST(copy_impl(other.sv()));
return *this;
}
String& String::operator=(String&& other)
{
BAN::deallocator(m_data);
move_impl(move(other));
return *this;
}
String& String::operator=(StringView other)
{
MUST(copy_impl(other));
return *this;
}
ErrorOr<void> String::push_back(char ch)
{
TRY(ensure_capacity(m_size + 2));
m_data[m_size] = ch;
m_size++;
m_data[m_size] = '\0';
return {};
}
ErrorOr<void> String::insert(char ch, size_type index)
{
ASSERT(index <= m_size);
TRY(ensure_capacity(m_size + 1 + 1));
memmove(m_data + index + 1, m_data + index, m_size - index);
m_data[index] = ch;
m_size += 1;
m_data[m_size] = '\0';
return {};
}
ErrorOr<void> String::insert(StringView other, size_type index)
{
ASSERT(index <= m_size);
TRY(ensure_capacity(m_size + other.size() + 1));
memmove(m_data + index + other.size(), m_data + index, m_size - index);
memcpy(m_data + index, other.data(), other.size());
m_size += other.size();
m_data[m_size] = '\0';
return {};
}
ErrorOr<void> String::append(StringView other)
{
TRY(ensure_capacity(m_size + other.size() + 1));
memcpy(m_data + m_size, other.data(), other.size());
m_size += other.size();
m_data[m_size] = '\0';
return {};
}
ErrorOr<void> String::append(const String& string)
{
TRY(append(string.sv()));
return {};
}
void String::pop_back()
{
ASSERT(m_size > 0);
m_size--;
m_data[m_size] = '\0';
}
void String::remove(size_type index)
{
erase(index, 1);
}
void String::erase(size_type index, size_type count)
{
ASSERT(index + count <= m_size);
memmove(m_data + index, m_data + index + count, m_size - index - count);
m_size -= count;
m_data[m_size] = '\0';
}
void String::clear()
{
m_size = 0;
m_data[0] = '\0';
}
char String::operator[](size_type index) const
{
ASSERT(index < m_size);
return m_data[index];
}
char& String::operator[](size_type index)
{
ASSERT(index < m_size);
return m_data[index];
}
bool String::operator==(const String& other) const
{
if (m_size != other.m_size)
return false;
return memcmp(m_data, other.m_data, m_size) == 0;
}
bool String::operator==(StringView other) const
{
if (m_size != other.size())
return false;
return memcmp(m_data, other.data(), m_size) == 0;
}
bool String::operator==(const char* other) const
{
for (size_type i = 0; i <= m_size; i++)
if (m_data[i] != other[i])
return false;
return true;
}
ErrorOr<void> String::resize(size_type size, char ch)
{
if (size < m_size)
{
m_data[size] = '\0';
m_size = size;
}
else if (size > m_size)
{
TRY(ensure_capacity(size + 1));
for (size_type i = m_size; i < size; i++)
m_data[i] = ch;
m_data[size] = '\0';
m_size = size;
}
m_size = size;
return {};
}
ErrorOr<void> String::reserve(size_type size)
{
TRY(ensure_capacity(size));
return {};
}
ErrorOr<void> String::shrink_to_fit()
{
size_type temp = m_capacity;
m_capacity = 0;
auto error_or = ensure_capacity(m_size);
if (error_or.is_error())
{
m_capacity = temp;
return error_or;
}
return {};
}
StringView String::sv() const
{
return StringView(*this);
}
bool String::empty() const
{
return m_size == 0;
}
String::size_type String::size() const
{
return m_size;
}
String::size_type String::capacity() const
{
return m_capacity;
}
const char* String::data() const
{
return m_data;
}
ErrorOr<void> String::ensure_capacity(size_type size)
{
if (m_capacity >= size)
return {};
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 2);
void* new_data = BAN::allocator(new_cap);
if (new_data == nullptr)
return Error::from_errno(ENOMEM);
if (m_data)
memcpy(new_data, m_data, m_size + 1);
BAN::deallocator(m_data);
m_data = (char*)new_data;
m_capacity = new_cap;
return {};
}
ErrorOr<void> String::copy_impl(StringView other)
{
TRY(ensure_capacity(other.size() + 1));
memcpy(m_data, other.data(), other.size());
m_size = other.size();
m_data[m_size] = '\0';
return {};
}
void String::move_impl(String&& other)
{
m_data = other.m_data;
m_size = other.m_size;
m_capacity = other.m_capacity;
other.m_data = nullptr;
other.m_size = 0;
other.m_capacity = 0;
}
}

View File

@ -1,143 +1,11 @@
#include <BAN/String.h>
#include <BAN/StringView.h>
#include <BAN/Vector.h>
#include <string.h>
namespace BAN
{
StringView::StringView()
{ }
StringView::StringView(const String& other)
: StringView(other.data(), other.size())
{ }
StringView::StringView(const char* string, size_type len)
{
if (len == size_type(-1))
len = strlen(string);
m_data = string;
m_size = len;
}
char StringView::operator[](size_type index) const
{
ASSERT(index < m_size);
return m_data[index];
}
bool StringView::operator==(const String& other) const
{
if (m_size != other.size())
return false;
return memcmp(m_data, other.data(), m_size) == 0;
}
bool StringView::operator==(StringView other) const
{
if (m_size != other.m_size)
return false;
return memcmp(m_data, other.m_data, m_size) == 0;
}
bool StringView::operator==(const char* other) const
{
if (memcmp(m_data, other, m_size))
return false;
return other[m_size] == '\0';
}
StringView StringView::substring(size_type index, size_type len) const
{
ASSERT(index <= m_size);
if (len == size_type(-1))
len = m_size - index;
ASSERT(len <= m_size - index); // weird order to avoid overflow
StringView result;
result.m_data = m_data + index;
result.m_size = len;
return result;
}
ErrorOr<Vector<StringView>> StringView::split(char delim, bool allow_empties)
{
// FIXME: Won't work while multithreading
static char s_delim = delim;
return split([](char c){ return c == s_delim; }, allow_empties);
}
ErrorOr<Vector<StringView>> StringView::split(bool(*comp)(char), bool allow_empties)
{
size_type count = 0;
{
size_type start = 0;
for (size_type i = 0; i < m_size; i++)
{
if (comp(m_data[i]))
{
if (allow_empties || start != i)
count++;
start = i + 1;
}
}
if (start != m_size)
count++;
}
Vector<StringView> result;
TRY(result.reserve(count));
size_type start = 0;
for (size_type i = 0; i < m_size; i++)
{
if (comp(m_data[i]))
{
if (allow_empties || start != i)
TRY(result.push_back(this->substring(start, i - start)));
start = i + 1;
}
}
if (start != m_size)
TRY(result.push_back(this->substring(start)));
return result;
}
char StringView::back() const
{
ASSERT(m_size > 0);
return m_data[m_size - 1];
}
char StringView::front() const
{
ASSERT(m_size > 0);
return m_data[0];
}
StringView::size_type StringView::count(char ch) const
{
size_type result = 0;
for (size_type i = 0; i < m_size; i++)
if (m_data[i] == ch)
result++;
return result;
}
bool StringView::empty() const
{
return m_size == 0;
}
StringView::size_type StringView::size() const
{
return m_size;
}
const char* StringView::data() const
{
return m_data;
}
}
}

71
BAN/BAN/Time.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <BAN/Time.h>
namespace BAN
{
static constexpr bool is_leap_year(uint64_t year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
static constexpr uint64_t leap_days_since_epoch(const BAN::Time& time)
{
uint64_t leap_years = 0;
for (uint32_t year = 1970; year < time.year; year++)
if (is_leap_year(year))
leap_years++;
if (is_leap_year(time.year) && time.month >= 3)
leap_years++;
return leap_years;
}
static constexpr uint64_t month_days[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
uint64_t to_unix_time(const BAN::Time& time)
{
uint64_t years = time.year - 1970;
uint64_t days = years * 365 + month_days[time.month - 1] + leap_days_since_epoch(time) + (time.day - 1);
uint64_t hours = days * 24 + time.hour;
uint64_t minutes = hours * 60 + time.minute;
uint64_t seconds = minutes * 60 + time.second;
return seconds;
}
BAN::Time from_unix_time(uint64_t unix_time)
{
BAN::Time time {};
time.second = unix_time % 60; unix_time /= 60;
time.minute = unix_time % 60; unix_time /= 60;
time.hour = unix_time % 24; unix_time /= 24;
uint64_t total_days = unix_time;
time.week_day = (total_days + 4) % 7 + 1;
time.year = 1970;
while (total_days >= 365U + is_leap_year(time.year))
{
total_days -= 365U + is_leap_year(time.year);
time.year++;
}
bool is_leap_day = is_leap_year(time.year) && total_days == month_days[2];
bool had_leap_day = is_leap_year(time.year) && total_days > month_days[2];
for (time.month = 1; time.month < 12; time.month++)
if (total_days < month_days[time.month] + (is_leap_day || had_leap_day))
break;
time.day = total_days - month_days[time.month - 1] + !had_leap_day;
return time;
}
}

15
BAN/CMakeLists.txt Normal file
View File

@ -0,0 +1,15 @@
set(BAN_SOURCES
BAN/Assert.cpp
BAN/New.cpp
BAN/StringView.cpp
BAN/Time.cpp
)
add_library(ban ${BAN_SOURCES})
target_link_options(ban PRIVATE -nolibc)
banan_link_library(ban libc)
set_target_properties(ban PROPERTIES OUTPUT_NAME libban)
banan_install_headers(ban)
install(TARGETS ban OPTIONAL)

View File

@ -1,89 +0,0 @@
DEFAULT_HOST!=../default-host.sh
HOST?=DEFAULT_HOST
HOSTARCH!=../target-triplet-to-arch.sh $(HOST)
CFLAGS?=-O2 -g
CPPFLAGS?=
LDFLAGS?=
LIBS?=
DESTDIR?=
PREFIX?=/usr/local
EXEC_PREFIX?=$(PREFIX)
INCLUDEDIR?=$(PREFIX)/include
LIBDIR?=$(EXEC_PREFIX)/lib
CFLAGS:=$(CFLAGS) -Iinclude -ffreestanding -Wall -Wextra -Werror=return-type
CPPFLAGS:=$(CPPFLAGS)
LIBBANK_CFLAGS:=$(CFLAGS) -D__is_kernel -Iinclude -ffreestanding -Wall -Wextra
LIBBANK_CPPFLAGS:=$(CPPFLAGS) -fno-rtti -fno-exceptions
ARCHDIR=arch/$(HOSTARCH)
include $(ARCHDIR)/make.config
CFLAGS:=$(CFLAGS) $(ARCH_CFLAGS)
CPPFLAGS:=$(CPPFLAGS) $(ARCH_CPPFLAGS)
LIBBANK_CFLAGS:=$(LIBBANK_CFLAGS) $(KERNEL_ARCH_CFLAGS)
LIBBANK_CPPFLAGS:=$(LIBBANK_CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS)
BUILDDIR=$(abspath build)
FREEOBJS= \
$(ARCH_FREEOBJS) \
BAN/Memory.o \
BAN/String.o \
BAN/StringView.o \
HOSTEDOBJS=\
$(ARCH_HOSTEDOBJS) \
OBJS=\
$(FREEOBJS) \
$(HOSTEDOBJS) \
LIBBANK_OBJS=$(FREEOBJS:.o=.bank.o)
BINARIES=libbank.a
.PHONY: all always clean install install-headers install-libs
.SUFFIXES: .o .bank.o .cpp .S
all: $(BINARIES)
libban.a: always $(OBJS)
cd $(BUILDDIR) && $(AR) rcs $@ $(OBJS)
libbank.a: always $(LIBBANK_OBJS)
cd $(BUILDDIR) && $(AR) rcs $@ $(LIBBANK_OBJS)
.cpp.o:
$(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(CFLAGS) $(CPPFLAGS)
.S.o:
$(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(CFLAGS) $(CPPFLAGS)
.cpp.bank.o:
$(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(LIBBANK_CFLAGS) $(LIBBANK_CPPFLAGS)
.S.bank.o:
$(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(LIBBANK_CFLAGS) $(LIBBANK_CPPFLAGS)
clean:
rm -rf $(BUILDDIR)
always:
mkdir -p $(BUILDDIR)/BAN
install: install-headers install-libs
install-headers:
mkdir -p $(DESTDIR)$(INCLUDEDIR)
cp -R --preserve=timestamps include/. $(DESTDIR)$(INCLUDEDIR)/.
install-libs: $(BINARIES)
mkdir -p $(DESTDIR)$(LIBDIR)
cp $(BUILDDIR)/$(BINARIES) $(DESTDIR)$(LIBDIR)
-include $(OBJS:.o=.d)
-include $(LIBBANK_OBJS:.o=.d)

View File

@ -1,8 +0,0 @@
ARCH_CFLAGS=
ARCH_CPPFLAGS=
KERNEL_ARCH_CFLAGS=
KERNEL_ARCH_CPPFLAGS=
ARCH_FREEOBJS=\
ARCH_HOSTEDOBJS=\

View File

@ -1,8 +0,0 @@
ARCH_CFLAGS=
ARCH_CPPFLAGS=
KERNEL_ARCH_CFLAGS=
KERNEL_ARCH_CPPFLAGS=
ARCH_FREEOBJS=\
ARCH_HOSTEDOBJS=\

View File

@ -1,6 +1,7 @@
#pragma once
#include <BAN/Errors.h>
#include <BAN/Iterators.h>
#include <BAN/Span.h>
#include <stddef.h>
@ -13,11 +14,18 @@ namespace BAN
public:
using size_type = decltype(S);
using value_type = T;
using iterator = IteratorSimple<T, Array>;
using const_iterator = ConstIteratorSimple<T, Array>;
public:
Array();
Array() = default;
Array(const T&);
iterator begin() { return iterator(m_data); }
iterator end() { return iterator(m_data + size()); }
const_iterator begin() const { return const_iterator(m_data); }
const_iterator end() const { return const_iterator(m_data + size()); }
const T& operator[](size_type) const;
T& operator[](size_type);
@ -26,18 +34,17 @@ namespace BAN
const T& front() const;
T& front();
Span<T> span() { return Span(m_data, size()); }
const Span<T> span() const { return Span(m_data, size()); }
constexpr size_type size() const;
private:
T m_data[S];
};
const T* data() const { return m_data; }
T* data() { return m_data; }
template<typename T, size_t S>
Array<T, S>::Array()
{
for (size_type i = 0; i < S; i++)
m_data[i] = T();
}
private:
T m_data[S] {};
};
template<typename T, size_t S>
Array<T, S>::Array(const T& value)
@ -94,4 +101,4 @@ namespace BAN
return S;
}
}
}

View File

@ -1,11 +1,14 @@
#pragma once
#if defined(__is_kernel)
#include <kernel/Panic.h>
#define ASSERT(cond) do { if (!(cond)) Kernel::panic("ASSERT("#cond") failed"); } while(false)
#define ASSERT_NOT_REACHED() Kernel::panic("ASSERT_NOT_REACHED() failed")
#else
#include <assert.h>
#define ASSERT(cond) assert((cond) && "ASSERT("#cond") failed")
#define ASSERT_NOT_REACHED() assert(false && "ASSERT_NOT_REACHED() failed")
#endif
#define __ban_assert_stringify_helper(s) #s
#define __ban_assert_stringify(s) __ban_assert_stringify_helper(s)
#define ASSERT(cond) \
(__builtin_expect(!(cond), 0) \
? __ban_assertion_failed(__FILE__ ":" __ban_assert_stringify(__LINE__), "ASSERT(" #cond ") failed") \
: (void)0)
#define ASSERT_NOT_REACHED() \
__ban_assertion_failed(__FILE__ ":" __ban_assert_stringify(__LINE__), "ASSERT_NOT_REACHED() reached")
[[noreturn]] void __ban_assertion_failed(const char* location, const char* msg);

69
BAN/include/BAN/Atomic.h Normal file
View File

@ -0,0 +1,69 @@
#pragma once
namespace BAN
{
enum MemoryOrder
{
memory_order_relaxed = __ATOMIC_RELAXED,
memory_order_consume = __ATOMIC_CONSUME,
memory_order_acquire = __ATOMIC_ACQUIRE,
memory_order_release = __ATOMIC_RELEASE,
memory_order_acq_rel = __ATOMIC_ACQ_REL,
memory_order_seq_cst = __ATOMIC_SEQ_CST,
};
template<typename T, MemoryOrder MEM_ORDER = MemoryOrder::memory_order_seq_cst>
requires requires { __atomic_always_lock_free(sizeof(T), 0); }
class Atomic
{
Atomic(const Atomic&) = delete;
Atomic(Atomic&&) = delete;
Atomic& operator=(const Atomic&) volatile = delete;
Atomic& operator=(Atomic&&) volatile = delete;
public:
constexpr Atomic() : m_value(0) {}
constexpr Atomic(T val) : m_value(val) {}
inline T load(MemoryOrder mem_order = MEM_ORDER) const volatile { return __atomic_load_n(&m_value, mem_order); }
inline void store(T val, MemoryOrder mem_order = MEM_ORDER) volatile { __atomic_store_n(&m_value, val, mem_order); }
inline T operator=(T val) volatile { store(val); return val; }
inline operator T() const volatile { return load(); }
inline T operator+=(T val) volatile { return __atomic_add_fetch(&m_value, val, MEM_ORDER); }
inline T operator-=(T val) volatile { return __atomic_sub_fetch(&m_value, val, MEM_ORDER); }
inline T operator&=(T val) volatile { return __atomic_and_fetch(&m_value, val, MEM_ORDER); }
inline T operator^=(T val) volatile { return __atomic_xor_fetch(&m_value, val, MEM_ORDER); }
inline T operator|=(T val) volatile { return __atomic_or_fetch(&m_value, val, MEM_ORDER); }
inline T operator--() volatile { return __atomic_sub_fetch(&m_value, 1, MEM_ORDER); }
inline T operator++() volatile { return __atomic_add_fetch(&m_value, 1, MEM_ORDER); }
inline T operator--(int) volatile { return __atomic_fetch_sub(&m_value, 1, MEM_ORDER); }
inline T operator++(int) volatile { return __atomic_fetch_add(&m_value, 1, MEM_ORDER); }
inline bool compare_exchange(T& expected, T desired, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_compare_exchange_n(&m_value, &expected, desired, false, mem_order, mem_order); }
inline T exchange(T desired, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_exchange_n(&m_value, desired, mem_order); };
inline T add_fetch (T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_add_fetch (&m_value, val, mem_order); }
inline T sub_fetch (T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_sub_fetch (&m_value, val, mem_order); }
inline T and_fetch (T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_and_fetch (&m_value, val, mem_order); }
inline T xor_fetch (T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_xor_fetch (&m_value, val, mem_order); }
inline T or_fetch (T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_or_fetch (&m_value, val, mem_order); }
inline T nand_fetch(T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_nand_fetch(&m_value, val, mem_order); }
inline T fetch_add (T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_fetch_add (&m_value, val, mem_order); }
inline T fetch_sub (T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_fetch_sub (&m_value, val, mem_order); }
inline T fetch_and (T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_fetch_and (&m_value, val, mem_order); }
inline T fetch_xor (T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_fetch_xor (&m_value, val, mem_order); }
inline T fetch_or (T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_fetch__or (&m_value, val, mem_order); }
inline T fetch_nand(T val, MemoryOrder mem_order = MEM_ORDER) volatile { return __atomic_nfetch_and(&m_value, val, mem_order); }
private:
T m_value;
};
}

12
BAN/include/BAN/Bitcast.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
namespace BAN
{
template<typename To, typename From>
constexpr To bit_cast(const From& from)
{
return __builtin_bit_cast(To, from);
}
}

124
BAN/include/BAN/ByteSpan.h Normal file
View File

@ -0,0 +1,124 @@
#pragma once
#include <BAN/Span.h>
#include <stdint.h>
namespace BAN
{
template<bool CONST>
class ByteSpanGeneral
{
public:
using value_type = maybe_const_t<CONST, uint8_t>;
using size_type = size_t;
public:
ByteSpanGeneral() = default;
ByteSpanGeneral(value_type* data, size_type size)
: m_data(data)
, m_size(size)
{ }
template<bool SRC_CONST>
ByteSpanGeneral(const ByteSpanGeneral<SRC_CONST>& other) requires(CONST || !SRC_CONST)
: m_data(other.data())
, m_size(other.size())
{ }
template<bool SRC_CONST>
ByteSpanGeneral(ByteSpanGeneral<SRC_CONST>&& other) requires(CONST || !SRC_CONST)
: m_data(other.data())
, m_size(other.size())
{
other.clear();
}
template<typename T>
ByteSpanGeneral(const Span<T>& other) requires(is_same_v<T, uint8_t> || (is_same_v<T, const uint8_t> && CONST))
: m_data(other.data())
, m_size(other.size())
{ }
template<typename T>
ByteSpanGeneral(Span<T>&& other) requires(is_same_v<T, uint8_t> || (is_same_v<T, const uint8_t> && CONST))
: m_data(other.data())
, m_size(other.size())
{
other.clear();
}
template<bool SRC_CONST>
ByteSpanGeneral& operator=(const ByteSpanGeneral<SRC_CONST>& other) requires(CONST || !SRC_CONST)
{
m_data = other.data();
m_size = other.size();
return *this;
}
template<bool SRC_CONST>
ByteSpanGeneral& operator=(ByteSpanGeneral<SRC_CONST>&& other) requires(CONST || !SRC_CONST)
{
m_data = other.data();
m_size = other.size();
other.clear();
return *this;
}
template<typename S>
static ByteSpanGeneral from(S& value) requires(CONST || !is_const_v<S>)
{
return ByteSpanGeneral(reinterpret_cast<value_type*>(&value), sizeof(S));
}
template<typename S>
S& as() const requires(!CONST || is_const_v<S>)
{
ASSERT(m_data);
ASSERT(m_size >= sizeof(S));
return *reinterpret_cast<S*>(m_data);
}
template<typename S>
Span<S> as_span() const requires(!CONST || is_const_v<S>)
{
ASSERT(m_data);
return Span<S>(reinterpret_cast<S*>(m_data), m_size / sizeof(S));
}
[[nodiscard]] ByteSpanGeneral slice(size_type offset, size_type length = size_type(-1)) const
{
ASSERT(m_data);
ASSERT(m_size >= offset);
if (length == size_type(-1))
length = m_size - offset;
ASSERT(m_size >= offset + length);
return ByteSpanGeneral(m_data + offset, length);
}
value_type& operator[](size_type offset) const
{
ASSERT(offset < m_size);
return m_data[offset];
}
value_type* data() const { return m_data; }
bool empty() const { return m_size == 0; }
size_type size() const { return m_size; }
void clear()
{
m_data = nullptr;
m_size = 0;
}
private:
value_type* m_data { nullptr };
size_type m_size { 0 };
friend class ByteSpanGeneral<!CONST>;
};
using ByteSpan = ByteSpanGeneral<false>;
using ConstByteSpan = ByteSpanGeneral<true>;
}

View File

@ -0,0 +1,132 @@
#pragma once
#include <BAN/Assert.h>
#include <BAN/Move.h>
#include <BAN/PlacementNew.h>
#include <stdint.h>
#include <stddef.h>
namespace BAN
{
template<typename T, size_t S>
class CircularQueue
{
public:
using size_type = size_t;
using value_type = T;
public:
CircularQueue() = default;
~CircularQueue();
void push(const T&);
void push(T&&);
template<typename... Args>
void emplace(Args&&... args) requires is_constructible_v<T, Args...>;
void pop();
const T& front() const;
T& front();
const T& back() const;
T& back();
size_type size() const { return m_size; }
bool empty() const { return size() == 0; }
bool full() const { return size() == capacity(); }
static constexpr size_type capacity() { return S; }
private:
T* element_at(size_type);
const T* element_at(size_type) const;
private:
alignas(T) uint8_t m_storage[sizeof(T) * capacity()];
size_type m_first { 0 };
size_type m_size { 0 };
};
template<typename T, size_t S>
CircularQueue<T, S>::~CircularQueue()
{
for (size_type i = 0; i < m_size; i++)
element_at((m_first + i) % capacity())->~T();
}
template<typename T, size_t S>
void CircularQueue<T, S>::push(const T& value)
{
emplace(BAN::move(T(value)));
}
template<typename T, size_t S>
void CircularQueue<T, S>::push(T&& value)
{
emplace(BAN::move(value));
}
template<typename T, size_t S>
template<typename... Args>
void CircularQueue<T, S>::emplace(Args&&... args) requires is_constructible_v<T, Args...>
{
ASSERT(!full());
new (element_at(((m_first + m_size) % capacity()))) T(BAN::forward<Args>(args)...);
m_size++;
}
template<typename T, size_t S>
void CircularQueue<T, S>::pop()
{
ASSERT(!empty());
element_at(m_first)->~T();
m_first = (m_first + 1) % capacity();
m_size--;
}
template<typename T, size_t S>
const T& CircularQueue<T, S>::front() const
{
ASSERT(!empty());
return *element_at(m_first);
}
template<typename T, size_t S>
T& CircularQueue<T, S>::front()
{
ASSERT(!empty());
return *element_at(m_first);
}
template<typename T, size_t S>
const T& CircularQueue<T, S>::back() const
{
ASSERT(!empty());
return *element_at((m_first + m_size - 1) % capacity());
}
template<typename T, size_t S>
T& CircularQueue<T, S>::back()
{
ASSERT(!empty());
return *element_at((m_first + m_size - 1) % capacity());
}
template<typename T, size_t S>
const T* CircularQueue<T, S>::element_at(size_type index) const
{
ASSERT(index < capacity());
return (const T*)(m_storage + index * sizeof(T));
}
template<typename T, size_t S>
T* CircularQueue<T, S>::element_at(size_type index)
{
ASSERT(index < capacity());
return (T*)(m_storage + index * sizeof(T));
}
}

55
BAN/include/BAN/Debug.h Normal file
View File

@ -0,0 +1,55 @@
#pragma once
#if __is_kernel
#include <kernel/Debug.h>
#else
#include <BAN/Formatter.h>
#include <stdio.h>
#define __debug_putchar [](int c) { putc(c, stddbg); }
#define dprintln(...) \
do { \
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
BAN::Formatter::print(__debug_putchar,"\r\n"); \
fflush(stddbg); \
} while (false)
#define dwarnln(...) \
do { \
BAN::Formatter::print(__debug_putchar, "\e[33m"); \
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
BAN::Formatter::print(__debug_putchar, "\e[m\r\n"); \
fflush(stddbg); \
} while(false)
#define derrorln(...) \
do { \
BAN::Formatter::print(__debug_putchar, "\e[31m"); \
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
BAN::Formatter::print(__debug_putchar, "\e[m\r\n"); \
fflush(stddbg); \
} while(false)
#define dprintln_if(cond, ...) \
do { \
if constexpr(cond) \
dprintln(__VA_ARGS__); \
} while(false)
#define dwarnln_if(cond, ...) \
do { \
if constexpr(cond) \
dwarnln(__VA_ARGS__); \
} while(false)
#define derrorln_if(cond, ...) \
do { \
if constexpr(cond) \
derrorln(__VA_ARGS__); \
} while(false)
#endif

View File

@ -0,0 +1,125 @@
#pragma once
#include <BAN/Traits.h>
#include <stddef.h>
namespace BAN
{
template<integral T>
constexpr T swap_endianness(T value)
{
if constexpr(sizeof(T) == 1)
return value;
if constexpr(sizeof(T) == 2)
return (((value >> 8) & 0xFF) << 0)
| (((value >> 0) & 0xFF) << 8);
if constexpr(sizeof(T) == 4)
return (((value >> 24) & 0xFF) << 0)
| (((value >> 16) & 0xFF) << 8)
| (((value >> 8) & 0xFF) << 16)
| (((value >> 0) & 0xFF) << 24);
if constexpr(sizeof(T) == 8)
return (((value >> 56) & 0xFF) << 0)
| (((value >> 48) & 0xFF) << 8)
| (((value >> 40) & 0xFF) << 16)
| (((value >> 32) & 0xFF) << 24)
| (((value >> 24) & 0xFF) << 32)
| (((value >> 16) & 0xFF) << 40)
| (((value >> 8) & 0xFF) << 48)
| (((value >> 0) & 0xFF) << 56);
T result { 0 };
for (size_t i = 0; i < sizeof(T); i++)
result |= ((value >> (i * 8)) & 0xFF) << ((sizeof(T) - i - 1) * 8);
return result;
}
template<integral T>
constexpr T host_to_little_endian(T value)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return value;
#else
return swap_endianness(value);
#endif
}
template<integral T>
constexpr T little_endian_to_host(T value)
{
return host_to_little_endian(value);
}
template<integral T>
constexpr T host_to_big_endian(T value)
{
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return value;
#else
return swap_endianness(value);
#endif
}
template<integral T>
constexpr T big_endian_to_host(T value)
{
return host_to_big_endian(value);
}
template<integral T>
struct LittleEndian
{
constexpr LittleEndian()
: raw(0)
{ }
constexpr LittleEndian(T value)
: raw(host_to_little_endian(value))
{ }
constexpr operator T() const
{
return host_to_little_endian(raw);
}
private:
T raw;
};
template<integral T>
struct BigEndian
{
constexpr BigEndian()
: raw(0)
{ }
constexpr BigEndian(T value)
: raw(host_to_big_endian(value))
{ }
constexpr operator T() const
{
return host_to_big_endian(raw);
}
private:
T raw;
};
template<integral T>
using NetworkEndian = BigEndian<T>;
template<integral T>
constexpr T host_to_network_endian(T value)
{
return host_to_big_endian(value);
}
template<integral T>
constexpr T network_endian_to_host(T value)
{
return big_endian_to_host(value);
}
}

View File

@ -1,67 +1,104 @@
#pragma once
#include <BAN/Formatter.h>
#include <BAN/NoCopyMove.h>
#include <BAN/Variant.h>
#include <errno.h>
#include <string.h>
#if defined(__is_kernel)
#ifdef __is_kernel
#include <kernel/Panic.h>
#define MUST(expr) ({ auto e = expr; if (e.is_error()) Kernel::panic("{}", e.error()); e.release_value(); })
#include <kernel/Errors.h>
#define MUST(expr) ({ auto&& e = expr; if (e.is_error()) Kernel::panic("{}", e.error()); e.release_value(); })
#define MUST_REF(expr) *({ auto&& e = expr; if (e.is_error()) Kernel::panic("{}", e.error()); &e.release_value(); })
#else
#define MUST(expr) ({ auto e = expr; assert(!e.is_error()); e.release_value(); })
#include <BAN/Debug.h>
#define MUST(expr) ({ auto&& e = expr; if (e.is_error()) { derrorln("MUST(" #expr "): {}", e.error()); __builtin_trap(); } e.release_value(); })
#define MUST_REF(expr) *({ auto&& e = expr; if (e.is_error()) { derrorln("MUST(" #expr "): {}", e.error()); __builtin_trap(); } &e.release_value(); })
#endif
#define TRY(expr) ({ auto e = expr; if (e.is_error()) return e.release_error(); e.release_value(); })
#define TRY(expr) ({ auto&& e = expr; if (e.is_error()) return e.release_error(); e.release_value(); })
#define TRY_REF(expr) *({ auto&& e = expr; if (e.is_error()) return e.release_error(); &e.release_value(); })
namespace BAN
{
class Error
{
#ifdef __is_kernel
private:
static constexpr uint64_t kernel_error_mask = uint64_t(1) << 63;
#endif
public:
static Error from_c_string(const char* message)
#ifdef __is_kernel
static Error from_error_code(Kernel::ErrorCode error)
{
Error result;
strncpy(result.m_message, message, sizeof(Error::m_message));
result.m_message[sizeof(Error::m_message) - 1] = '\0';
result.m_error_code = 0xFF;
return result;
return Error((uint64_t)error | kernel_error_mask);
}
template<typename... Args>
static Error from_format(const char* format, Args&&... args)
#else
template<size_t N>
consteval static Error from_literal(const char (&message)[N])
{
char buffer[sizeof(Error::m_message)] {};
size_t index = 0;
auto putc = [&](char ch)
{
if (index < sizeof(buffer) - 1)
buffer[index++] = ch;
};
Formatter::print(putc, format, forward<Args>(args)...);
return from_c_string(buffer);
return Error(message);
}
#endif
static Error from_errno(int error)
{
Error result;
strncpy(result.m_message, strerror(error), sizeof(Error::m_message));
result.m_message[sizeof(Error::m_message) - 1] = '\0';
result.m_error_code = error;
return result;
return Error(error);
}
uint8_t get_error_code() const { return m_error_code; }
const char* get_message() const { return m_message; }
#ifdef __is_kernel
Kernel::ErrorCode kernel_error() const
{
return (Kernel::ErrorCode)(m_error_code & ~kernel_error_mask);
}
bool is_kernel_error() const
{
return m_error_code & kernel_error_mask;
}
#endif
constexpr uint64_t get_error_code() const { return m_error_code; }
const char* get_message() const
{
#ifdef __is_kernel
if (m_error_code & kernel_error_mask)
return Kernel::error_string(kernel_error());
#else
if (m_message)
return m_message;
#endif
if (auto* desc = strerrordesc_np(m_error_code))
return desc;
return "Unknown error";
}
private:
char m_message[128];
uint8_t m_error_code;
constexpr Error(uint64_t error)
: m_error_code(error)
{}
#ifndef __is_kernel
constexpr Error(const char* message)
: m_message(message)
{}
#endif
uint64_t m_error_code { 0 };
#ifndef __is_kernel
const char* m_message { nullptr };
#endif
};
template<typename T>
class [[nodiscard]] ErrorOr
{
BAN_NON_COPYABLE(ErrorOr);
public:
ErrorOr(const T& value)
: m_data(value)
@ -75,20 +112,55 @@ namespace BAN
ErrorOr(Error&& error)
: m_data(move(error))
{}
ErrorOr(ErrorOr&& other)
: m_data(move(other.m_data))
{}
ErrorOr& operator=(ErrorOr&& other)
{
m_data = move(other.m_data);
return *this;
}
bool is_error() const { return m_data.template is<Error>(); }
bool is_error() const { return m_data.template has<Error>(); }
const Error& error() const { return m_data.template get<Error>(); }
Error& error() { return m_data.template get<Error>(); }
const T& value() const { return m_data.template get<T>(); }
T& value() { return m_data.template get<T>(); }
Error release_error() { return move(error()); m_data.clear(); }
Error release_error() { return move(error()); m_data.clear(); }
T release_value() { return move(value()); m_data.clear(); }
private:
Variant<Error, T> m_data;
};
template<lvalue_reference T>
class [[nodiscard]] ErrorOr<T>
{
public:
ErrorOr(T value)
{
m_data.template set<T>(value);
}
ErrorOr(Error&& error)
: m_data(move(error))
{ }
ErrorOr(const Error& error)
: m_data(error)
{ }
bool is_error() const { return m_data.template has<Error>(); }
Error& error() { return m_data.template get<Error>(); }
const Error& error() const { return m_data.template get<Error>(); }
T value() { return m_data.template get<T>(); }
Error release_error() { return move(error()); m_data.clear(); }
T release_value() { return value(); m_data.clear(); }
private:
Variant<Error, T> m_data;
};
template<>
class [[nodiscard]] ErrorOr<void>
{
@ -102,12 +174,12 @@ namespace BAN
const Error& error() const { return m_data; }
void value() { }
Error release_error() { return move(m_data); m_data = Error(); }
void release_value() { m_data = Error(); }
Error release_error() { return move(m_data); }
void release_value() { }
private:
Error m_data;
bool m_has_error = false;
Error m_data { Error::from_errno(0) };
bool m_has_error { false };
};
}
@ -115,11 +187,8 @@ namespace BAN
namespace BAN::Formatter
{
template<typename F>
void print_argument(F putc, const Error& error, const ValueFormat&)
void print_argument(F putc, const Error& error, const ValueFormat& format)
{
if (error.get_error_code() == 0xFF)
print(putc, error.get_message());
else
print(putc, "{} ({})", error.get_message(), error.get_error_code());
print_argument(putc, error.get_message(), format);
}
}

View File

@ -10,27 +10,23 @@ namespace BAN::Formatter
struct ValueFormat;
template<typename F>
static void print(F putc, const char* format);
template<typename F, typename Arg, typename... Args>
static void print(F putc, const char* format, Arg&& arg, Args&&... args);
template<typename F, typename... Args>
static void println(F putc, const char* format, Args&&... args);
concept PrintableArguments = requires(F putc, Args&&... args, const ValueFormat& format)
{
(print_argument(putc, BAN::forward<Args>(args), format), ...);
};
template<typename F, typename T>
static void print_argument(F putc, T value, const ValueFormat& format);
inline void print_argument(F putc, T value, const ValueFormat& format);
namespace detail
{
template<typename F, typename T>
static size_t parse_format_and_print_argument(F putc, const char* format, T&& arg);
inline size_t parse_format_and_print_argument(F putc, const char* format, T&& arg);
}
/*
IMPLEMENTATION
*/
@ -40,11 +36,12 @@ namespace BAN::Formatter
int base = 10;
int percision = 3;
int fill = 0;
char fill_char = '0';
bool upper = false;
};
template<typename F>
void print(F putc, const char* format)
inline void print(F putc, const char* format)
{
while (*format)
{
@ -53,8 +50,8 @@ namespace BAN::Formatter
}
}
template<typename F, typename Arg, typename... Args>
void print(F putc, const char* format, Arg&& arg, Args&&... args)
template<typename F, typename Arg, typename... Args> requires PrintableArguments<F, Arg, Args...>
inline void print(F putc, const char* format, Arg&& arg, Args&&... args)
{
while (*format && *format != '{')
{
@ -72,7 +69,7 @@ namespace BAN::Formatter
}
template<typename F, typename... Args>
void println(F putc, const char* format, Args&&... args)
inline void println(F putc, const char* format, Args&&... args)
{
print(putc, format, args...);
putc('\n');
@ -82,7 +79,7 @@ namespace BAN::Formatter
{
template<typename F, typename Arg>
size_t parse_format_and_print_argument(F putc, const char* format, Arg&& argument)
inline size_t parse_format_and_print_argument(F putc, const char* format, Arg&& argument)
{
ValueFormat value_format;
@ -95,6 +92,12 @@ namespace BAN::Formatter
if (!format[i] || format[i] == '}')
break;
if (format[i] == ' ')
{
value_format.fill_char = ' ';
i++;
}
if ('0' <= format[i] && format[i] <= '9')
{
int fill = 0;
@ -144,7 +147,7 @@ namespace BAN::Formatter
return i + 1;
}
static char value_to_base_char(uint8_t value, int base, bool upper)
inline char value_to_base_char(uint8_t value, int base, bool upper)
{
if (base <= 10)
return value + '0';
@ -158,12 +161,13 @@ namespace BAN::Formatter
}
template<typename F, typename T>
void print_integer(F putc, T value, const ValueFormat& format)
inline void print_integer(F putc, T value, const ValueFormat& format)
{
if (value == 0)
{
for (int i = 0; i < format.fill || i < 1; i++)
putc('0');
for (int i = 0; i < format.fill - 1; i++)
putc(format.fill_char);
putc('0');
return;
}
@ -189,16 +193,16 @@ namespace BAN::Formatter
}
while (ptr >= buffer + sizeof(buffer) - format.fill)
*(--ptr) = '0';
*(--ptr) = format.fill_char;
if (sign)
*(--ptr) = '-';
print(putc, ptr);
}
template<typename F, typename T>
void print_floating(F putc, T value, const ValueFormat& format)
inline void print_floating(F putc, T value, const ValueFormat& format)
{
int64_t int_part = (int64_t)value;
T frac_part = value - (T)int_part;
@ -206,10 +210,10 @@ namespace BAN::Formatter
frac_part = -frac_part;
print_integer(putc, int_part, format);
if (format.percision > 0)
putc('.');
for (int i = 0; i < format.percision; i++)
{
frac_part *= format.base;
@ -221,7 +225,7 @@ namespace BAN::Formatter
}
template<typename F>
void print_pointer(F putc, void* ptr, const ValueFormat& format)
inline void print_pointer(F putc, void* ptr, const ValueFormat& format)
{
uintptr_t value = (uintptr_t)ptr;
print(putc, "0x");
@ -232,21 +236,18 @@ namespace BAN::Formatter
}
/*
TEMPLATE SPECIALIZATIONS
*/
template<typename F, integral T> void print_argument(F putc, T value, const ValueFormat& format) { detail::print_integer(putc, value, format); }
template<typename F, floating_point T> void print_argument(F putc, T value, const ValueFormat& format) { detail::print_floating(putc, value, format); }
template<typename F, pointer T> void print_argument(F putc, T value, const ValueFormat& format) { detail::print_pointer(putc, (void*)value, format); }
template<typename F, integral T> inline void print_argument(F putc, T value, const ValueFormat& format) { detail::print_integer(putc, value, format); }
template<typename F, floating_point T> inline void print_argument(F putc, T value, const ValueFormat& format) { detail::print_floating(putc, value, format); }
template<typename F, pointer T> inline void print_argument(F putc, T value, const ValueFormat& format) { detail::print_pointer(putc, (void*)value, format); }
template<typename F> void print_argument(F putc, char value, const ValueFormat&) { putc(value); }
template<typename F> void print_argument(F putc, bool value, const ValueFormat&) { print(putc, value ? "true" : "false"); }
template<typename F> void print_argument(F putc, const char* value, const ValueFormat&) { print(putc, value);}
//template<typename F> void print_argument(F putc, signed char value, const ValueFormat& format) { detail::print_integer(putc, value, format); }
//template<typename F> void print_argument(F putc, unsigned char value, const ValueFormat& format) { detail::print_integer(putc, value, format); }
//template<typename F, typename T> void print_argument(F putc, T* value, const ValueFormat& format) { detail::print_pointer(putc, (void*)value, format); }
template<typename F> inline void print_argument(F putc, char value, const ValueFormat&) { putc(value); }
template<typename F> inline void print_argument(F putc, bool value, const ValueFormat&) { print(putc, value ? "true" : "false"); }
template<typename F> inline void print_argument(F putc, const char* value, const ValueFormat&) { print(putc, value); }
template<typename F> inline void print_argument(F putc, char* value, const ValueFormat&) { print(putc, value); }
}

View File

@ -1,11 +1,12 @@
#pragma once
#include <BAN/Traits.h>
#include <stddef.h>
namespace BAN
{
template<typename, size_t> class Array;
template<typename> class ErrorOr;
template<typename> class Function;
@ -14,5 +15,6 @@ namespace BAN
class StringView;
template<typename> class Vector;
template<typename> class LinkedList;
template<typename... Ts> requires (!is_const_v<Ts> && ...) class Variant;
}

View File

@ -2,7 +2,7 @@
#include <BAN/Errors.h>
#include <BAN/Move.h>
#include <BAN/Memory.h>
#include <BAN/PlacementNew.h>
namespace BAN
{
@ -20,19 +20,19 @@ namespace BAN
new (m_storage) CallablePointer(function);
}
template<typename Own>
Function(Ret(Own::*function)(Args...), Own* owner)
Function(Ret(Own::*function)(Args...), Own& owner)
{
static_assert(sizeof(CallableMember<Own>) <= m_size);
new (m_storage) CallableMember<Own>(function, owner);
}
template<typename Own>
Function(Ret(Own::*function)(Args...) const, const Own* owner)
Function(Ret(Own::*function)(Args...) const, const Own& owner)
{
static_assert(sizeof(CallableMemberConst<Own>) <= m_size);
new (m_storage) CallableMemberConst<Own>(function, owner);
}
template<typename Lambda>
Function(Lambda lambda)
Function(Lambda lambda) requires requires(Lambda lamda, Args&&... args) { { lambda(forward<Args>(args)...) } -> BAN::same_as<Ret>; }
{
static_assert(sizeof(CallableLambda<Lambda>) <= m_size);
new (m_storage) CallableLambda<Lambda>(lambda);
@ -43,10 +43,10 @@ namespace BAN
clear();
}
Ret operator()(Args... args)
Ret operator()(Args... args) const
{
ASSERT(*this);
return reinterpret_cast<CallableBase*>(m_storage)->call(forward<Args>(args)...);
return reinterpret_cast<const CallableBase*>(m_storage)->call(forward<Args>(args)...);
}
operator bool() const
@ -56,7 +56,7 @@ namespace BAN
return true;
return false;
}
void clear()
{
if (*this)
@ -70,7 +70,7 @@ namespace BAN
struct CallableBase
{
virtual ~CallableBase() {}
virtual Ret call(Args...) = 0;
virtual Ret call(Args...) const = 0;
};
struct CallablePointer : public CallableBase
@ -79,7 +79,7 @@ namespace BAN
: m_function(function)
{ }
virtual Ret call(Args... args) override
virtual Ret call(Args... args) const override
{
return m_function(forward<Args>(args)...);
}
@ -91,36 +91,36 @@ namespace BAN
template<typename Own>
struct CallableMember : public CallableBase
{
CallableMember(Ret(Own::*function)(Args...), Own* owner)
CallableMember(Ret(Own::*function)(Args...), Own& owner)
: m_owner(owner)
, m_function(function)
{ }
virtual Ret call(Args... args) override
virtual Ret call(Args... args) const override
{
return (m_owner->*m_function)(forward<Args>(args)...);
return (m_owner.*m_function)(forward<Args>(args)...);
}
private:
Own* m_owner = nullptr;
Own& m_owner;
Ret(Own::*m_function)(Args...) = nullptr;
};
template<typename Own>
struct CallableMemberConst : public CallableBase
{
CallableMemberConst(Ret(Own::*function)(Args...) const, const Own* owner)
CallableMemberConst(Ret(Own::*function)(Args...) const, const Own& owner)
: m_owner(owner)
, m_function(function)
{ }
virtual Ret call(Args... args) override
virtual Ret call(Args... args) const override
{
return (m_owner->*m_function)(forward<Args>(args)...);
return (m_owner.*m_function)(forward<Args>(args)...);
}
private:
const Own* m_owner = nullptr;
const Own& m_owner;
Ret(Own::*m_function)(Args...) const = nullptr;
};
@ -131,7 +131,7 @@ namespace BAN
: m_lambda(lambda)
{ }
virtual Ret call(Args... args) override
virtual Ret call(Args... args) const override
{
return m_lambda(forward<Args>(args)...);
}
@ -141,8 +141,8 @@ namespace BAN
};
private:
static constexpr size_t m_size = sizeof(void*) * 4;
static constexpr size_t m_size = sizeof(void*) * 8;
alignas(CallableBase) uint8_t m_storage[m_size] { 0 };
};
}
}

73
BAN/include/BAN/GUID.h Normal file
View File

@ -0,0 +1,73 @@
#pragma once
#include <BAN/Optional.h>
#include <BAN/String.h>
#include <string.h>
namespace BAN
{
struct GUID
{
uint32_t component1 { 0 };
uint16_t component2 { 0 };
uint16_t component3 { 0 };
uint8_t component45[8] { };
bool operator==(const GUID& other) const
{
return memcmp(this, &other, sizeof(GUID)) == 0;
}
BAN::ErrorOr<BAN::String> to_string() const
{
char buffer[37];
char* ptr = buffer;
const auto append_hex_nibble =
[&ptr](uint8_t nibble)
{
if (nibble < 10)
*ptr++ = '0' + nibble;
else
*ptr++ = 'A' + nibble - 10;
};
const auto append_hex_byte =
[&append_hex_nibble](uint8_t byte)
{
append_hex_nibble(byte >> 4);
append_hex_nibble(byte & 0xF);
};
append_hex_byte((component1 >> 24) & 0xFF);
append_hex_byte((component1 >> 16) & 0xFF);
append_hex_byte((component1 >> 8) & 0xFF);
append_hex_byte((component1 >> 0) & 0xFF);
*ptr++ = '-';
append_hex_byte((component2 >> 8) & 0xFF);
append_hex_byte((component2 >> 0) & 0xFF);
*ptr++ = '-';
append_hex_byte((component3 >> 8) & 0xFF);
append_hex_byte((component3 >> 0) & 0xFF);
*ptr++ = '-';
append_hex_byte(component45[0]);
append_hex_byte(component45[1]);
*ptr++ = '-';
append_hex_byte(component45[2]);
append_hex_byte(component45[3]);
append_hex_byte(component45[4]);
append_hex_byte(component45[5]);
append_hex_byte(component45[6]);
append_hex_byte(component45[7]);
*ptr = '\0';
BAN::String guid;
TRY(guid.append(buffer));
return BAN::move(guid);
}
};
static_assert(sizeof(GUID) == 16);
}

View File

@ -47,4 +47,4 @@ namespace BAN
}
};
}
}

View File

@ -10,10 +10,31 @@ namespace BAN
template<typename Key, typename T, typename HASH = BAN::hash<Key>>
class HashMap
{
public:
struct Entry
{
template<typename... Args>
Entry(const Key& key, Args&&... args) requires is_constructible_v<T, Args...>
: key(key)
, value(forward<Args>(args)...)
{}
template<typename... Args>
Entry(Key&& key, Args&&... args) requires is_constructible_v<T, Args...>
: key(BAN::move(key))
, value(forward<Args>(args)...)
{}
Key key;
T value;
};
public:
using size_type = size_t;
using key_type = Key;
using value_type = T;
using iterator = IteratorDouble<Entry, Vector, LinkedList, HashMap>;
using const_iterator = ConstIteratorDouble<Entry, Vector, LinkedList, HashMap>;
public:
HashMap() = default;
@ -24,45 +45,61 @@ namespace BAN
HashMap<Key, T, HASH>& operator=(const HashMap<Key, T, HASH>&);
HashMap<Key, T, HASH>& operator=(HashMap<Key, T, HASH>&&);
ErrorOr<void> insert(const Key&, const T&);
ErrorOr<void> insert(const Key&, T&&);
ErrorOr<iterator> insert(const Key& key, const T& value) { return emplace(key, value); }
ErrorOr<iterator> insert(const Key& key, T&& value) { return emplace(key, move(value)); }
ErrorOr<iterator> insert(Key&& key, const T& value) { return emplace(move(key), value); }
ErrorOr<iterator> insert(Key&& key, T&& value) { return emplace(move(key), move(value)); }
ErrorOr<iterator> insert_or_assign(const Key& key, const T& value) { return emplace_or_assign(key, value); }
ErrorOr<iterator> insert_or_assign(const Key& key, T&& value) { return emplace_or_assign(key, move(value)); }
ErrorOr<iterator> insert_or_assign(Key&& key, const T& value) { return emplace_or_assign(move(key), value); }
ErrorOr<iterator> insert_or_assign(Key&& key, T&& value) { return emplace_or_assign(move(key), move(value)); }
template<typename... Args>
ErrorOr<void> emplace(const Key&, Args&&...);
ErrorOr<iterator> emplace(const Key& key, Args&&... args) requires is_constructible_v<T, Args...>
{ return emplace(Key(key), forward<Args>(args)...); }
template<typename... Args>
ErrorOr<iterator> emplace(Key&&, Args&&...) requires is_constructible_v<T, Args...>;
template<typename... Args>
ErrorOr<iterator> emplace_or_assign(const Key& key, Args&&... args) requires is_constructible_v<T, Args...>
{ return emplace_or_assign(Key(key), forward<Args>(args)...); }
template<typename... Args>
ErrorOr<iterator> emplace_or_assign(Key&&, Args&&...) requires is_constructible_v<T, Args...>;
iterator begin() { return iterator(m_buckets.end(), m_buckets.begin()); }
iterator end() { return iterator(m_buckets.end(), m_buckets.end()); }
const_iterator begin() const { return const_iterator(m_buckets.end(), m_buckets.begin()); }
const_iterator end() const { return const_iterator(m_buckets.end(), m_buckets.end()); }
ErrorOr<void> reserve(size_type);
void remove(const Key&);
void remove(iterator it);
void clear();
T& operator[](const Key&);
const T& operator[](const Key&) const;
iterator find(const Key& key);
const_iterator find(const Key& key) const;
bool contains(const Key&) const;
bool empty() const;
size_type size() const;
private:
struct Entry
{
template<typename... Args>
Entry(const Key& key, Args&&... args)
: key(key)
, value(forward<Args>(args)...)
{}
Key key;
T value;
};
private:
ErrorOr<void> rebucket(size_type);
LinkedList<Entry>& get_bucket(const Key&);
const LinkedList<Entry>& get_bucket(const Key&) const;
Vector<LinkedList<Entry>>::iterator get_bucket_iterator(const Key&);
Vector<LinkedList<Entry>>::const_iterator get_bucket_iterator(const Key&) const;
private:
Vector<LinkedList<Entry>> m_buckets;
size_type m_size = 0;
friend iterator;
};
template<typename Key, typename T, typename HASH>
@ -103,29 +140,36 @@ namespace BAN
}
template<typename Key, typename T, typename HASH>
ErrorOr<void> HashMap<Key, T, HASH>::insert(const Key& key, const T& value)
template<typename... Args>
ErrorOr<typename HashMap<Key, T, HASH>::iterator> HashMap<Key, T, HASH>::emplace(Key&& key, Args&&... args) requires is_constructible_v<T, Args...>
{
return insert(key, move(T(value)));
}
ASSERT(!contains(key));
TRY(rebucket(m_size + 1));
template<typename Key, typename T, typename HASH>
ErrorOr<void> HashMap<Key, T, HASH>::insert(const Key& key, T&& value)
{
return emplace(key, move(value));
auto bucket_it = get_bucket_iterator(key);
TRY(bucket_it->emplace_back(move(key), forward<Args>(args)...));
m_size++;
return iterator(m_buckets.end(), bucket_it, prev(bucket_it->end(), 1));
}
template<typename Key, typename T, typename HASH>
template<typename... Args>
ErrorOr<void> HashMap<Key, T, HASH>::emplace(const Key& key, Args&&... args)
ErrorOr<typename HashMap<Key, T, HASH>::iterator> HashMap<Key, T, HASH>::emplace_or_assign(Key&& key, Args&&... args) requires is_constructible_v<T, Args...>
{
ASSERT(!contains(key));
TRY(rebucket(m_size + 1));
auto& bucket = get_bucket(key);
auto result = bucket.emplace_back(key, forward<Args>(args)...);
if (result.is_error())
return Error::from_errno(ENOMEM);
m_size++;
return {};
if (empty())
return emplace(move(key), forward<Args>(args)...);
auto bucket_it = get_bucket_iterator(key);
for (auto entry_it = bucket_it->begin(); entry_it != bucket_it->end(); entry_it++)
{
if (entry_it->key != key)
continue;
entry_it->value = T(forward<Args>(args)...);
return iterator(m_buckets.end(), bucket_it, entry_it);
}
return emplace(move(key), forward<Args>(args)...);
}
template<typename Key, typename T, typename HASH>
@ -138,17 +182,16 @@ namespace BAN
template<typename Key, typename T, typename HASH>
void HashMap<Key, T, HASH>::remove(const Key& key)
{
if (empty()) return;
auto& bucket = get_bucket(key);
for (auto it = bucket.begin(); it != bucket.end(); it++)
{
if (it->key == key)
{
bucket.remove(it);
m_size--;
return;
}
}
auto it = find(key);
if (it != end())
remove(it);
}
template<typename Key, typename T, typename HASH>
void HashMap<Key, T, HASH>::remove(iterator it)
{
it.outer_current()->remove(it.inner_current());
m_size--;
}
template<typename Key, typename T, typename HASH>
@ -166,7 +209,7 @@ namespace BAN
for (Entry& entry : bucket)
if (entry.key == key)
return entry.value;
ASSERT(false);
ASSERT_NOT_REACHED();
}
template<typename Key, typename T, typename HASH>
@ -177,18 +220,37 @@ namespace BAN
for (const Entry& entry : bucket)
if (entry.key == key)
return entry.value;
ASSERT(false);
ASSERT_NOT_REACHED();
}
template<typename Key, typename T, typename HASH>
typename HashMap<Key, T, HASH>::iterator HashMap<Key, T, HASH>::find(const Key& key)
{
if (empty())
return end();
auto bucket_it = get_bucket_iterator(key);
for (auto it = bucket_it->begin(); it != bucket_it->end(); it++)
if (it->key == key)
return iterator(m_buckets.end(), bucket_it, it);
return end();
}
template<typename Key, typename T, typename HASH>
typename HashMap<Key, T, HASH>::const_iterator HashMap<Key, T, HASH>::find(const Key& key) const
{
if (empty())
return end();
auto bucket_it = get_bucket_iterator(key);
for (auto it = bucket_it->begin(); it != bucket_it->end(); it++)
if (it->key == key)
return const_iterator(m_buckets.end(), bucket_it, it);
return end();
}
template<typename Key, typename T, typename HASH>
bool HashMap<Key, T, HASH>::contains(const Key& key) const
{
if (empty()) return false;
const auto& bucket = get_bucket(key);
for (const Entry& entry : bucket)
if (entry.key == key)
return true;
return false;
return find(key) != end();
}
template<typename Key, typename T, typename HASH>
@ -211,18 +273,14 @@ namespace BAN
size_type new_bucket_count = BAN::Math::max<size_type>(bucket_count, m_buckets.size() * 2);
Vector<LinkedList<Entry>> new_buckets;
if (new_buckets.resize(new_bucket_count).is_error())
return Error::from_errno(ENOMEM);
// NOTE: We have to copy the old entries to the new entries and not move
// since we might run out of memory half way through.
TRY(new_buckets.resize(new_bucket_count));
for (auto& bucket : m_buckets)
{
for (Entry& entry : bucket)
for (auto it = bucket.begin(); it != bucket.end();)
{
size_type bucket_index = HASH()(entry.key) % new_buckets.size();
if (new_buckets[bucket_index].push_back(entry).is_error())
return Error::from_errno(ENOMEM);
size_type new_bucket_index = HASH()(it->key) % new_buckets.size();
it = bucket.move_element_to_other_linked_list(new_buckets[new_bucket_index], new_buckets[new_bucket_index].end(), it);
}
}
@ -233,17 +291,29 @@ namespace BAN
template<typename Key, typename T, typename HASH>
LinkedList<typename HashMap<Key, T, HASH>::Entry>& HashMap<Key, T, HASH>::get_bucket(const Key& key)
{
ASSERT(!m_buckets.empty());
auto index = HASH()(key) % m_buckets.size();
return m_buckets[index];
return *get_bucket_iterator(key);
}
template<typename Key, typename T, typename HASH>
const LinkedList<typename HashMap<Key, T, HASH>::Entry>& HashMap<Key, T, HASH>::get_bucket(const Key& key) const
{
ASSERT(!m_buckets.empty());
auto index = HASH()(key) % m_buckets.size();
return m_buckets[index];
return *get_bucket_iterator(key);
}
}
template<typename Key, typename T, typename HASH>
Vector<LinkedList<typename HashMap<Key, T, HASH>::Entry>>::iterator HashMap<Key, T, HASH>::get_bucket_iterator(const Key& key)
{
ASSERT(!m_buckets.empty());
auto index = HASH()(key) % m_buckets.size();
return next(m_buckets.begin(), index);
}
template<typename Key, typename T, typename HASH>
Vector<LinkedList<typename HashMap<Key, T, HASH>::Entry>>::const_iterator HashMap<Key, T, HASH>::get_bucket_iterator(const Key& key) const
{
ASSERT(!m_buckets.empty());
auto index = HASH()(key) % m_buckets.size();
return next(m_buckets.begin(), index);
}
}

View File

@ -2,6 +2,8 @@
#include <BAN/Errors.h>
#include <BAN/Hash.h>
#include <BAN/Iterators.h>
#include <BAN/LinkedList.h>
#include <BAN/Math.h>
#include <BAN/Move.h>
#include <BAN/Vector.h>
@ -9,24 +11,22 @@
namespace BAN
{
template<typename T, typename HASH>
class HashSetIterator;
template<typename T, typename HASH = hash<T>>
class HashSet
{
public:
using value_type = T;
using size_type = hash_t;
using const_iterator = HashSetIterator<T, HASH>;
using size_type = size_t;
using iterator = IteratorDouble<T, Vector, LinkedList, HashSet>;
using const_iterator = ConstIteratorDouble<T, Vector, LinkedList, HashSet>;
public:
HashSet() = default;
HashSet(const HashSet<T, HASH>&);
HashSet(HashSet<T, HASH>&&);
HashSet(const HashSet&);
HashSet(HashSet&&);
HashSet<T, HASH>& operator=(const HashSet<T, HASH>&);
HashSet<T, HASH>& operator=(HashSet<T, HASH>&&);
HashSet& operator=(const HashSet&);
HashSet& operator=(HashSet&&);
ErrorOr<void> insert(const T&);
ErrorOr<void> insert(T&&);
@ -35,8 +35,10 @@ namespace BAN
ErrorOr<void> reserve(size_type);
const_iterator begin() const { return const_iterator(this, m_buckets.begin()); }
const_iterator end() const { return const_iterator(this, m_buckets.end()); }
iterator begin() { return iterator(m_buckets.end(), m_buckets.begin()); }
iterator end() { return iterator(m_buckets.end(), m_buckets.end()); }
const_iterator begin() const { return const_iterator(m_buckets.end(), m_buckets.begin()); }
const_iterator end() const { return const_iterator(m_buckets.end(), m_buckets.end()); }
bool contains(const T&) const;
@ -45,57 +47,23 @@ namespace BAN
private:
ErrorOr<void> rebucket(size_type);
Vector<T>& get_bucket(const T&);
const Vector<T>& get_bucket(const T&) const;
LinkedList<T>& get_bucket(const T&);
const LinkedList<T>& get_bucket(const T&) const;
private:
Vector<Vector<T>> m_buckets;
Vector<LinkedList<T>> m_buckets;
size_type m_size = 0;
friend class HashSetIterator<T, HASH>;
};
template<typename T, typename HASH>
class HashSetIterator
{
public:
HashSetIterator() = default;
HashSetIterator(const HashSetIterator<T, HASH>&);
HashSetIterator<T, HASH>& operator++();
HashSetIterator<T, HASH> operator++(int);
const T& operator*() const;
const T* operator->() const;
bool operator==(const HashSetIterator<T, HASH>&) const;
bool operator!=(const HashSetIterator<T, HASH>&) const;
operator bool() const { return m_owner && m_current_bucket; }
private:
HashSetIterator(const HashSet<T, HASH>* owner, Vector<Vector<T>>::const_iterator bucket);
void find_next();
private:
const HashSet<T, HASH>* m_owner = nullptr;
Vector<Vector<T>>::const_iterator m_current_bucket;
Vector<T>::const_iterator m_current_key;
friend class HashSet<T, HASH>;
};
template<typename T, typename HASH>
HashSet<T, HASH>::HashSet(const HashSet<T, HASH>& other)
HashSet<T, HASH>::HashSet(const HashSet& other)
: m_buckets(other.m_buckets)
, m_size(other.m_size)
{
}
template<typename T, typename HASH>
HashSet<T, HASH>::HashSet(HashSet<T, HASH>&& other)
HashSet<T, HASH>::HashSet(HashSet&& other)
: m_buckets(move(other.m_buckets))
, m_size(other.m_size)
{
@ -103,7 +71,7 @@ namespace BAN
}
template<typename T, typename HASH>
HashSet<T, HASH>& HashSet<T, HASH>::operator=(const HashSet<T, HASH>& other)
HashSet<T, HASH>& HashSet<T, HASH>::operator=(const HashSet& other)
{
clear();
m_buckets = other.m_buckets;
@ -112,7 +80,7 @@ namespace BAN
}
template<typename T, typename HASH>
HashSet<T, HASH>& HashSet<T, HASH>::operator=(HashSet<T, HASH>&& other)
HashSet<T, HASH>& HashSet<T, HASH>::operator=(HashSet&& other)
{
clear();
m_buckets = move(other.m_buckets);
@ -143,15 +111,15 @@ namespace BAN
void HashSet<T, HASH>::remove(const T& key)
{
if (empty()) return;
Vector<T>& bucket = get_bucket(key);
for (size_type i = 0; i < bucket.size(); i++)
auto& bucket = get_bucket(key);
for (auto it = bucket.begin(); it != bucket.end(); it++)
{
if (bucket[i] == key)
if (*it == key)
{
bucket.remove(i);
bucket.remove(it);
m_size--;
break;
}
}
}
}
@ -194,20 +162,17 @@ namespace BAN
if (m_buckets.size() >= bucket_count)
return {};
size_type new_bucket_count = BAN::Math::max<size_type>(bucket_count, m_buckets.size() * 2);
Vector<Vector<T>> new_buckets;
size_type new_bucket_count = Math::max<size_type>(bucket_count, m_buckets.size() * 2);
Vector<LinkedList<T>> new_buckets;
if (new_buckets.resize(new_bucket_count).is_error())
return Error::from_errno(ENOMEM);
// NOTE: We have to copy the old keys to the new keys and not move
// since we might run out of memory half way through.
for (Vector<T>& bucket : m_buckets)
for (auto& bucket : m_buckets)
{
for (T& key : bucket)
for (auto it = bucket.begin(); it != bucket.end();)
{
size_type bucket_index = HASH()(key) % new_buckets.size();
if (new_buckets[bucket_index].push_back(key).is_error())
return Error::from_errno(ENOMEM);
size_type new_bucket_index = HASH()(*it) % new_buckets.size();
it = bucket.move_element_to_other_linked_list(new_buckets[new_bucket_index], new_buckets[new_bucket_index].end(), it);
}
}
@ -216,7 +181,7 @@ namespace BAN
}
template<typename T, typename HASH>
Vector<T>& HashSet<T, HASH>::get_bucket(const T& key)
LinkedList<T>& HashSet<T, HASH>::get_bucket(const T& key)
{
ASSERT(!m_buckets.empty());
size_type index = HASH()(key) % m_buckets.size();
@ -224,85 +189,11 @@ namespace BAN
}
template<typename T, typename HASH>
const Vector<T>& HashSet<T, HASH>::get_bucket(const T& key) const
const LinkedList<T>& HashSet<T, HASH>::get_bucket(const T& key) const
{
ASSERT(!m_buckets.empty());
size_type index = HASH()(key) % m_buckets.size();
return m_buckets[index];
}
template<typename T, typename HASH>
HashSetIterator<T, HASH>& HashSetIterator<T, HASH>::operator++()
{
ASSERT(*this);
if (m_current_key == m_current_bucket->end())
m_current_bucket++;
else
m_current_key++;
find_next();
return *this;
}
template<typename T, typename HASH>
HashSetIterator<T, HASH> HashSetIterator<T, HASH>::operator++(int)
{
auto temp = *this;
++(*this);
return temp;
}
template<typename T, typename HASH>
const T& HashSetIterator<T, HASH>::operator*() const
{
ASSERT(m_owner && m_current_bucket && m_current_key);
return *m_current_key;
}
template<typename T, typename HASH>
const T* HashSetIterator<T, HASH>::operator->() const
{
return &**this;
}
template<typename T, typename HASH>
bool HashSetIterator<T, HASH>::operator==(const HashSetIterator<T, HASH>& other) const
{
if (!m_owner || m_owner != other.m_owner)
return false;
return m_current_bucket == other.m_current_bucket
&& m_current_key == other.m_current_key;
}
template<typename T, typename HASH>
bool HashSetIterator<T, HASH>::operator!=(const HashSetIterator<T, HASH>& other) const
{
return !(*this == other);
}
template<typename T, typename HASH>
HashSetIterator<T, HASH>::HashSetIterator(const HashSet<T, HASH>* owner, Vector<Vector<T>>::const_iterator bucket)
: m_owner(owner)
, m_current_bucket(bucket)
{
if (m_current_bucket != m_owner->m_buckets.end())
m_current_key = m_current_bucket->begin();
find_next();
}
template<typename T, typename HASH>
void HashSetIterator<T, HASH>::find_next()
{
ASSERT(m_owner && m_current_bucket);
while (m_current_bucket != m_owner->m_buckets.end())
{
if (m_current_key && m_current_key != m_current_bucket->end())
return;
m_current_bucket++;
m_current_key = m_current_bucket->begin();
}
m_current_key = typename Vector<T>::const_iterator();
}
}
}

75
BAN/include/BAN/IPv4.h Normal file
View File

@ -0,0 +1,75 @@
#pragma once
#include <BAN/Endianness.h>
#include <BAN/Formatter.h>
#include <BAN/Hash.h>
namespace BAN
{
struct IPv4Address
{
constexpr IPv4Address(uint32_t u32_address)
{
raw = u32_address;
}
constexpr IPv4Address(uint8_t oct1, uint8_t oct2, uint8_t oct3, uint8_t oct4)
{
octets[0] = oct1;
octets[1] = oct2;
octets[2] = oct3;
octets[3] = oct4;
}
constexpr bool operator==(const IPv4Address& other) const
{
return raw == other.raw;
}
constexpr IPv4Address mask(const IPv4Address& other) const
{
return IPv4Address(raw & other.raw);
}
union
{
uint8_t octets[4];
uint32_t raw;
} __attribute__((packed));
};
static_assert(sizeof(IPv4Address) == 4);
template<>
struct hash<IPv4Address>
{
constexpr hash_t operator()(IPv4Address ipv4) const
{
return hash<uint32_t>()(ipv4.raw);
}
};
}
namespace BAN::Formatter
{
template<typename F>
void print_argument(F putc, const IPv4Address& ipv4, const ValueFormat&)
{
ValueFormat format {
.base = 10,
.percision = 0,
.fill = 0,
.upper = false,
};
print_argument(putc, ipv4.octets[0], format);
for (size_t i = 1; i < 4; i++)
{
putc('.');
print_argument(putc, ipv4.octets[i], format);
}
}
}

View File

@ -0,0 +1,12 @@
#pragma once
namespace BAN
{
enum class Iteration
{
Continue,
Break
};
}

330
BAN/include/BAN/Iterators.h Normal file
View File

@ -0,0 +1,330 @@
#pragma once
#include <BAN/Assert.h>
#include <BAN/Traits.h>
#include <stddef.h>
namespace BAN
{
template<typename It>
constexpr It next(It it, size_t count)
{
for (size_t i = 0; i < count; i++)
++it;
return it;
}
template<typename It>
requires requires(It it, size_t n) { requires is_same_v<decltype(it + n), It>; }
constexpr It next(It it, size_t count)
{
return it + count;
}
template<typename It>
constexpr It prev(It it, size_t count)
{
for (size_t i = 0; i < count; i++)
--it;
return it;
}
template<typename It>
requires requires(It it, size_t n) { requires is_same_v<decltype(it - n), It>; }
constexpr It prev(It it, size_t count)
{
return it - count;
}
template<typename It>
constexpr size_t distance(It it1, It it2)
{
size_t dist = 0;
while (it1 != it2)
{
++it1;
++dist;
}
return dist;
}
template<typename It>
requires requires(It it1, It it2) { requires is_integral_v<decltype(it2 - it1)>; }
constexpr size_t distance(It it1, It it2)
{
return it2 - it1;
}
template<typename T, typename Container, bool CONST>
class IteratorSimpleGeneral
{
public:
using value_type = T;
public:
constexpr IteratorSimpleGeneral() = default;
template<bool CONST2, typename = enable_if_t<CONST2 == CONST || CONST>>
constexpr IteratorSimpleGeneral(const IteratorSimpleGeneral<T, Container, CONST2>& other)
: m_pointer(other.m_pointer)
, m_valid(other.m_valid)
{
}
constexpr const T& operator*() const
{
ASSERT(m_pointer);
return *m_pointer;
}
template<bool CONST2 = CONST>
constexpr enable_if_t<!CONST2, T&> operator*()
{
ASSERT(*this);
ASSERT(m_pointer);
return *m_pointer;
}
constexpr const T* operator->() const
{
ASSERT(*this);
ASSERT(m_pointer);
return m_pointer;
}
template<bool CONST2 = CONST>
constexpr enable_if_t<!CONST2, T*> operator->()
{
ASSERT(*this);
ASSERT(m_pointer);
return m_pointer;
}
constexpr IteratorSimpleGeneral& operator++()
{
ASSERT(*this);
ASSERT(m_pointer);
++m_pointer;
return *this;
}
constexpr IteratorSimpleGeneral operator++(int)
{
auto temp = *this;
++(*this);
return temp;
}
constexpr IteratorSimpleGeneral& operator--()
{
ASSERT(*this);
ASSERT(m_pointer);
--m_pointer;
return *this;
}
constexpr IteratorSimpleGeneral operator--(int)
{
auto temp = *this;
--(*this);
return temp;
}
constexpr size_t operator-(const IteratorSimpleGeneral& other) const
{
ASSERT(*this && other);
return m_pointer - other.m_pointer;
}
constexpr IteratorSimpleGeneral operator+(size_t offset) const
{
return IteratorSimpleGeneral(m_pointer + offset);
}
constexpr IteratorSimpleGeneral operator-(size_t offset) const
{
return IteratorSimpleGeneral(m_pointer - offset);
}
constexpr bool operator<(const IteratorSimpleGeneral& other) const
{
ASSERT(*this);
return m_pointer < other.m_pointer;
}
constexpr bool operator==(const IteratorSimpleGeneral& other) const
{
ASSERT(*this);
return m_pointer == other.m_pointer;
}
constexpr bool operator!=(const IteratorSimpleGeneral& other) const
{
ASSERT(*this);
return !(*this == other);
}
constexpr explicit operator bool() const
{
return m_valid;
}
private:
constexpr IteratorSimpleGeneral(maybe_const_t<CONST, T>* pointer)
: m_pointer(pointer)
, m_valid(true)
{
}
private:
maybe_const_t<CONST, T>* m_pointer = nullptr;
bool m_valid = false;
friend IteratorSimpleGeneral<T, Container, !CONST>;
friend Container;
};
template<typename T, template<typename> typename OuterContainer, template<typename> typename InnerContainer, typename Container, bool CONST>
class IteratorDoubleGeneral
{
public:
using Inner = InnerContainer<T>;
using Outer = OuterContainer<Inner>;
using InnerIterator = either_or_t<CONST, typename Inner::const_iterator, typename Inner::iterator>;
using OuterIterator = either_or_t<CONST, typename Outer::const_iterator, typename Outer::iterator>;
using value_type = T;
public:
constexpr IteratorDoubleGeneral() = default;
template<bool CONST2, typename = enable_if_t<CONST2 == CONST || CONST>>
constexpr IteratorDoubleGeneral(const IteratorDoubleGeneral<T, OuterContainer, InnerContainer, Container, CONST2>& other)
: m_outer_end(other.m_outer_end)
, m_outer_current(other.m_outer_current)
, m_inner_current(other.m_inner_current)
{
}
constexpr const T& operator*() const
{
ASSERT(*this);
ASSERT(m_outer_current != m_outer_end);
ASSERT(m_inner_current);
return m_inner_current.operator*();
}
template<bool CONST2 = CONST>
constexpr enable_if_t<!CONST2, T&> operator*()
{
ASSERT(*this);
ASSERT(m_outer_current != m_outer_end);
ASSERT(m_inner_current);
return m_inner_current.operator*();
}
constexpr const T* operator->() const
{
ASSERT(*this);
ASSERT(m_outer_current != m_outer_end);
ASSERT(m_inner_current);
return m_inner_current.operator->();
}
template<bool CONST2 = CONST>
constexpr enable_if_t<!CONST2, T*> operator->()
{
ASSERT(*this);
ASSERT(m_outer_current != m_outer_end);
ASSERT(m_inner_current);
return m_inner_current.operator->();
}
constexpr IteratorDoubleGeneral& operator++()
{
ASSERT(*this);
ASSERT(m_outer_current != m_outer_end);
ASSERT(m_inner_current);
m_inner_current++;
find_valid_or_end();
return *this;
}
constexpr IteratorDoubleGeneral operator++(int)
{
auto temp = *this;
++(*this);
return temp;
}
constexpr bool operator==(const IteratorDoubleGeneral& other) const
{
ASSERT(*this && other);
if (m_outer_end != other.m_outer_end)
return false;
if (m_outer_current != other.m_outer_current)
return false;
if (m_outer_current == m_outer_end)
return true;
ASSERT(m_inner_current && other.m_inner_current);
return m_inner_current == other.m_inner_current;
}
constexpr bool operator!=(const IteratorDoubleGeneral& other) const
{
return !(*this == other);
}
constexpr explicit operator bool() const
{
return !!m_outer_current;
}
private:
constexpr IteratorDoubleGeneral(const OuterIterator& outer_end, const OuterIterator& outer_current)
: m_outer_end(outer_end)
, m_outer_current(outer_current)
{
if (outer_current != outer_end)
{
m_inner_current = m_outer_current->begin();
find_valid_or_end();
}
}
constexpr IteratorDoubleGeneral(const OuterIterator& outer_end, const OuterIterator& outer_current, const InnerIterator& inner_current)
: m_outer_end(outer_end)
, m_outer_current(outer_current)
, m_inner_current(inner_current)
{
find_valid_or_end();
}
constexpr void find_valid_or_end()
{
while (m_inner_current == m_outer_current->end())
{
m_outer_current++;
if (m_outer_current == m_outer_end)
break;
m_inner_current = m_outer_current->begin();
}
}
constexpr OuterIterator outer_current() { return m_outer_current; }
constexpr InnerIterator inner_current() { return m_inner_current; }
private:
OuterIterator m_outer_end;
OuterIterator m_outer_current;
InnerIterator m_inner_current;
friend class IteratorDoubleGeneral<T, OuterContainer, InnerContainer, Container, !CONST>;
friend Container;
};
template<typename T, typename Container>
using IteratorSimple = IteratorSimpleGeneral<T, Container, false>;
template<typename T, typename Container>
using ConstIteratorSimple = IteratorSimpleGeneral<T, Container, true>;
template<typename T, template<typename> typename OuterContainer, template<typename> typename InnerContainer, typename Container>
using IteratorDouble = IteratorDoubleGeneral<T, OuterContainer, InnerContainer, Container, false>;
template<typename T, template<typename> typename OuterContainer, template<typename> typename InnerContainer, typename Container>
using ConstIteratorDouble = IteratorDoubleGeneral<T, OuterContainer, InnerContainer, Container, true>;
}

156
BAN/include/BAN/Limits.h Normal file
View File

@ -0,0 +1,156 @@
#pragma once
#include <BAN/Traits.h>
#include <stdint.h>
namespace BAN
{
template<typename T>
class numeric_limits
{
public:
numeric_limits() = delete;
static inline constexpr T max()
{
if constexpr(is_same_v<T, char>)
return __SCHAR_MAX__;
if constexpr(is_same_v<T, signed char>)
return __SCHAR_MAX__;
if constexpr(is_same_v<T, unsigned char>)
return (T)__SCHAR_MAX__ * 2 + 1;
if constexpr(is_same_v<T, short>)
return __SHRT_MAX__;
if constexpr(is_same_v<T, int>)
return __INT_MAX__;
if constexpr(is_same_v<T, long>)
return __LONG_MAX__;
if constexpr(is_same_v<T, long long>)
return __LONG_LONG_MAX__;
if constexpr(is_same_v<T, unsigned short>)
return (T)__SHRT_MAX__ * 2 + 1;
if constexpr(is_same_v<T, unsigned int>)
return (T)__INT_MAX__ * 2 + 1;
if constexpr(is_same_v<T, unsigned long>)
return (T)__LONG_MAX__ * 2 + 1;
if constexpr(is_same_v<T, unsigned long long>)
return (T)__LONG_LONG_MAX__ * 2 + 1;
if constexpr(is_same_v<T, float>)
return __FLT_MAX__;
if constexpr(is_same_v<T, double>)
return __DBL_MAX__;
if constexpr(is_same_v<T, long double>)
return __LDBL_MAX__;
}
static inline constexpr T min()
{
if constexpr(is_signed_v<T> && is_integral_v<T>)
return -max() - 1;
if constexpr(is_unsigned_v<T> && is_integral_v<T>)
return 0;
if constexpr(is_same_v<T, float>)
return __FLT_MIN__;
if constexpr(is_same_v<T, double>)
return __DBL_MIN__;
if constexpr(is_same_v<T, long double>)
return __LDBL_MIN__;
}
static inline constexpr bool has_infinity()
{
if constexpr(is_same_v<T, float>)
return __FLT_HAS_INFINITY__;
if constexpr(is_same_v<T, double>)
return __DBL_HAS_INFINITY__;
if constexpr(is_same_v<T, long double>)
return __LDBL_HAS_INFINITY__;
return false;
}
static inline constexpr T infinity() requires(has_infinity())
{
if constexpr(is_same_v<T, float>)
return __builtin_inff();
if constexpr(is_same_v<T, double>)
return __builtin_inf();
if constexpr(is_same_v<T, long double>)
return __builtin_infl();
}
static inline constexpr bool has_quiet_NaN()
{
if constexpr(is_same_v<T, float>)
return __FLT_HAS_QUIET_NAN__;
if constexpr(is_same_v<T, double>)
return __DBL_HAS_QUIET_NAN__;
if constexpr(is_same_v<T, long double>)
return __LDBL_HAS_QUIET_NAN__;
return false;
}
static inline constexpr T quiet_NaN() requires(has_quiet_NaN())
{
if constexpr(is_same_v<T, float>)
return __builtin_nanf("");
if constexpr(is_same_v<T, double>)
return __builtin_nan("");
if constexpr(is_same_v<T, long double>)
return __builtin_nanl("");
}
static inline constexpr int max_exponent2()
{
static_assert(__FLT_RADIX__ == 2);
if constexpr(is_same_v<T, float>)
return __FLT_MAX_EXP__;
if constexpr(is_same_v<T, double>)
return __DBL_MAX_EXP__;
if constexpr(is_same_v<T, long double>)
return __LDBL_MAX_EXP__;
return 0;
}
static inline constexpr int max_exponent10()
{
if constexpr(is_same_v<T, float>)
return __FLT_MAX_10_EXP__;
if constexpr(is_same_v<T, double>)
return __DBL_MAX_10_EXP__;
if constexpr(is_same_v<T, long double>)
return __LDBL_MAX_10_EXP__;
return 0;
}
static inline constexpr int min_exponent2()
{
static_assert(__FLT_RADIX__ == 2);
if constexpr(is_same_v<T, float>)
return __FLT_MIN_EXP__;
if constexpr(is_same_v<T, double>)
return __DBL_MIN_EXP__;
if constexpr(is_same_v<T, long double>)
return __LDBL_MIN_EXP__;
return 0;
}
static inline constexpr int min_exponent10()
{
if constexpr(is_same_v<T, float>)
return __FLT_MIN_10_EXP__;
if constexpr(is_same_v<T, double>)
return __DBL_MIN_10_EXP__;
if constexpr(is_same_v<T, long double>)
return __LDBL_MIN_10_EXP__;
return 0;
}
};
}

View File

@ -1,15 +1,16 @@
#pragma once
#include <BAN/Errors.h>
#include <BAN/Memory.h>
#include <BAN/Move.h>
#include <BAN/New.h>
#include <BAN/PlacementNew.h>
namespace BAN
{
template<typename T, bool CONST>
class LinkedListIterator;
template<typename T>
class LinkedList
{
@ -21,11 +22,11 @@ namespace BAN
public:
LinkedList() = default;
LinkedList(const LinkedList<T>& other) { *this = other; }
LinkedList(const LinkedList<T>& other) requires is_copy_constructible_v<T> { *this = other; }
LinkedList(LinkedList<T>&& other) { *this = move(other); }
~LinkedList() { clear(); }
LinkedList<T>& operator=(const LinkedList<T>&);
LinkedList<T>& operator=(const LinkedList<T>&) requires is_copy_constructible_v<T>;
LinkedList<T>& operator=(LinkedList<T>&&);
ErrorOr<void> push_back(const T&);
@ -33,14 +34,16 @@ namespace BAN
ErrorOr<void> insert(iterator, const T&);
ErrorOr<void> insert(iterator, T&&);
template<typename... Args>
ErrorOr<void> emplace_back(Args&&...);
ErrorOr<void> emplace_back(Args&&...) requires is_constructible_v<T, Args...>;
template<typename... Args>
ErrorOr<void> emplace(iterator, Args&&...);
ErrorOr<void> emplace(iterator, Args&&...) requires is_constructible_v<T, Args...>;
void pop_back();
void remove(iterator);
iterator remove(iterator);
void clear();
iterator move_element_to_other_linked_list(LinkedList& dest_list, iterator dest_iter, iterator src_iter);
iterator begin() { return iterator(m_data, empty()); }
const_iterator begin() const { return const_iterator(m_data, empty()); }
iterator end() { return iterator(m_last, true); }
@ -64,7 +67,11 @@ namespace BAN
Node* prev;
};
ErrorOr<Node*> allocate_node() const;
template<typename... Args>
ErrorOr<Node*> allocate_node(Args&&...) const;
Node* remove_node(iterator);
void insert_node(iterator, Node*);
Node* m_data = nullptr;
Node* m_last = nullptr;
@ -114,10 +121,8 @@ namespace BAN
friend class LinkedListIterator<T, !CONST>;
};
template<typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& other)
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& other) requires is_copy_constructible_v<T>
{
clear();
for (const T& elem : other)
@ -138,6 +143,31 @@ namespace BAN
return *this;
}
template<typename T>
LinkedList<T>::Node* LinkedList<T>::remove_node(iterator iter)
{
ASSERT(!empty() && iter);
Node* node = iter.m_current;
Node* prev = node->prev;
Node* next = node->next;
(prev ? prev->next : m_data) = next;
(next ? next->prev : m_last) = prev;
m_size--;
return node;
}
template<typename T>
void LinkedList<T>::insert_node(iterator iter, Node* node)
{
Node* next = iter.m_past_end ? nullptr : iter.m_current;
Node* prev = next ? next->prev : m_last;
node->next = next;
node->prev = prev;
(prev ? prev->next : m_data) = node;
(next ? next->prev : m_last) = node;
m_size++;
}
template<typename T>
ErrorOr<void> LinkedList<T>::push_back(const T& value)
{
@ -159,59 +189,43 @@ namespace BAN
template<typename T>
ErrorOr<void> LinkedList<T>::insert(iterator iter, T&& value)
{
Node* next = iter.m_past_end ? nullptr : iter.m_current;
Node* prev = next ? next->prev : m_last;
Node* new_node = TRY(allocate_node());
new (&new_node->value) T(move(value));
new_node->next = next;
new_node->prev = prev;
(prev ? prev->next : m_data) = new_node;
(next ? next->prev : m_last) = new_node;
m_size++;
Node* new_node = TRY(allocate_node(move(value)));
insert_node(iter, new_node);
return {};
}
template<typename T>
template<typename... Args>
ErrorOr<void> LinkedList<T>::emplace_back(Args&&... args)
ErrorOr<void> LinkedList<T>::emplace_back(Args&&... args) requires is_constructible_v<T, Args...>
{
return emplace(end(), forward<Args>(args)...);
}
template<typename T>
template<typename... Args>
ErrorOr<void> LinkedList<T>::emplace(iterator iter, Args&&... args)
ErrorOr<void> LinkedList<T>::emplace(iterator iter, Args&&... args) requires is_constructible_v<T, Args...>
{
Node* next = iter.m_past_end ? nullptr : iter.m_current;
Node* prev = next ? next->prev : m_last;
Node* new_node = TRY(allocate_node());
new (&new_node->value) T(forward<Args>(args)...);
new_node->next = next;
new_node->prev = prev;
(prev ? prev->next : m_data) = new_node;
(next ? next->prev : m_last) = new_node;
m_size++;
Node* new_node = TRY(allocate_node(forward<Args>(args)...));
insert_node(iter, new_node);
return {};
}
template<typename T>
void LinkedList<T>::pop_back()
{
return remove(m_last);
ASSERT(!empty());
remove(iterator(m_last, false));
}
template<typename T>
void LinkedList<T>::remove(iterator iter)
LinkedList<T>::iterator LinkedList<T>::remove(iterator iter)
{
ASSERT(!empty() && iter);
Node* node = iter.m_current;
Node* prev = node->prev;
Node* node = remove_node(iter);
Node* next = node->next;
node->value.~T();
BAN::deallocator(node);
(prev ? prev->next : m_data) = next;
(next ? next->prev : m_last) = prev;
m_size--;
return next ? iterator(next, false) : iterator(m_last, true);
}
template<typename T>
@ -230,6 +244,16 @@ namespace BAN
m_size = 0;
}
template<typename T>
LinkedList<T>::iterator LinkedList<T>::move_element_to_other_linked_list(LinkedList& dest_list, iterator dest_iter, iterator src_iter)
{
ASSERT(!empty() && src_iter);
Node* node = remove_node(src_iter);
iterator ret = node->next ? iterator(node->next, false) : iterator(m_last, true);
dest_list.insert_node(dest_iter, node);
return ret;
}
template<typename T>
const T& LinkedList<T>::back() const
{
@ -284,16 +308,16 @@ namespace BAN
}
template<typename T>
ErrorOr<typename LinkedList<T>::Node*> LinkedList<T>::allocate_node() const
template<typename... Args>
ErrorOr<typename LinkedList<T>::Node*> LinkedList<T>::allocate_node(Args&&... args) const
{
Node* node = (Node*)BAN::allocator(sizeof(Node));
if (node == nullptr)
return Error::from_errno(ENOMEM);
return Error::from_errno(ENOMEM);
new (&node->value) T(forward<Args>(args)...);
return node;
}
template<typename T, bool CONST>
template<bool C>
LinkedListIterator<T, CONST>::LinkedListIterator(const LinkedListIterator<T, C>& other, enable_if_t<C == CONST || !C>*)
@ -378,7 +402,7 @@ namespace BAN
ASSERT(m_current);
return &m_current->value;
}
template<typename T, bool CONST>
bool LinkedListIterator<T, CONST>::operator==(const LinkedListIterator<T, CONST>& other) const
{
@ -399,4 +423,4 @@ namespace BAN
return m_current;
}
}
}

47
BAN/include/BAN/MAC.h Normal file
View File

@ -0,0 +1,47 @@
#pragma once
#include <BAN/Formatter.h>
namespace BAN
{
struct MACAddress
{
uint8_t address[6];
constexpr bool operator==(const MACAddress& other) const
{
return
address[0] == other.address[0] &&
address[1] == other.address[1] &&
address[2] == other.address[2] &&
address[3] == other.address[3] &&
address[4] == other.address[4] &&
address[5] == other.address[5];
}
};
}
namespace BAN::Formatter
{
template<typename F>
void print_argument(F putc, const MACAddress& mac, const ValueFormat&)
{
ValueFormat format {
.base = 16,
.percision = 0,
.fill = 2,
.upper = true,
};
print_argument(putc, mac.address[0], format);
for (size_t i = 1; i < 6; i++)
{
putc(':');
print_argument(putc, mac.address[i], format);
}
}
}

View File

@ -1,13 +1,20 @@
#pragma once
#include <BAN/Limits.h>
#include <BAN/Numbers.h>
#include <BAN/Traits.h>
#include <stddef.h>
#include <stdint.h>
#include <float.h>
namespace BAN::Math
{
template<typename T>
inline constexpr T abs(T x)
{
return x < 0 ? -x : x;
}
template<typename T>
inline constexpr T min(T a, T b)
{
@ -52,21 +59,385 @@ namespace BAN::Math
}
template<integral T>
inline constexpr T little_endian_to_host(const uint8_t* bytes)
inline constexpr bool is_power_of_two(T x)
{
T result = 0;
for (size_t i = 0; i < sizeof(T); i++)
result |= (T)bytes[i] << (i * 8);
return result;
if (x == 0)
return false;
return (x & (x - 1)) == 0;
}
template<integral T>
inline constexpr T big_endian_to_host(const uint8_t* bytes)
template<BAN::integral T>
static constexpr bool will_multiplication_overflow(T a, T b)
{
T result = 0;
for (size_t i = 0; i < sizeof(T); i++)
result |= (T)bytes[i] << (8 * (sizeof(T) - i - 1));
return result;
if (a == 0 || b == 0)
return false;
if ((a > 0) == (b > 0))
return a > BAN::numeric_limits<T>::max() / b;
else
return a < BAN::numeric_limits<T>::min() / b;
}
}
template<BAN::integral T>
static constexpr bool will_addition_overflow(T a, T b)
{
if (a > 0 && b > 0)
return a > BAN::numeric_limits<T>::max() - b;
if (a < 0 && b < 0)
return a < BAN::numeric_limits<T>::min() - b;
return false;
}
template<typename T>
requires is_same_v<T, unsigned int> || is_same_v<T, unsigned long> || is_same_v<T, unsigned long long>
inline constexpr T ilog2(T x)
{
if constexpr(is_same_v<T, unsigned int>)
return sizeof(T) * 8 - __builtin_clz(x) - 1;
if constexpr(is_same_v<T, unsigned long>)
return sizeof(T) * 8 - __builtin_clzl(x) - 1;
return sizeof(T) * 8 - __builtin_clzll(x) - 1;
}
template<floating_point T>
inline constexpr T floor(T x)
{
if constexpr(is_same_v<T, float>)
return __builtin_floorf(x);
if constexpr(is_same_v<T, double>)
return __builtin_floor(x);
if constexpr(is_same_v<T, long double>)
return __builtin_floorl(x);
}
template<floating_point T>
inline constexpr T ceil(T x)
{
if constexpr(is_same_v<T, float>)
return __builtin_ceilf(x);
if constexpr(is_same_v<T, double>)
return __builtin_ceil(x);
if constexpr(is_same_v<T, long double>)
return __builtin_ceill(x);
}
template<floating_point T>
inline constexpr T round(T x)
{
if (x == (T)0.0)
return x;
if (x > (T)0.0)
return floor<T>(x + (T)0.5);
return ceil<T>(x - (T)0.5);
}
template<floating_point T>
inline constexpr T trunc(T x)
{
if constexpr(is_same_v<T, float>)
return __builtin_truncf(x);
if constexpr(is_same_v<T, double>)
return __builtin_trunc(x);
if constexpr(is_same_v<T, long double>)
return __builtin_truncl(x);
}
template<floating_point T>
inline constexpr T rint(T x)
{
asm("frndint" : "+t"(x));
return x;
}
template<floating_point T>
inline constexpr T fmod(T a, T b)
{
asm(
"1:"
"fprem;"
"fnstsw %%ax;"
"testb $4, %%ah;"
"jne 1b;"
: "+t"(a)
: "u"(b)
: "ax"
);
return a;
}
template<floating_point T>
static T modf(T x, T* iptr)
{
const T frac = BAN::Math::fmod<T>(x, 1);
*iptr = x - frac;
return frac;
}
template<floating_point T>
inline constexpr T frexp(T num, int* exp)
{
if (num == 0.0)
{
*exp = 0;
return 0.0;
}
T _exp;
asm("fxtract" : "+t"(num), "=u"(_exp));
*exp = (int)_exp + 1;
return num / (T)2.0;
}
template<floating_point T>
inline constexpr T copysign(T x, T y)
{
if ((x < (T)0.0) != (y < (T)0.0))
x = -x;
return x;
}
namespace detail
{
template<floating_point T>
inline constexpr T fyl2x(T x, T y)
{
asm("fyl2x" : "+t"(x) : "u"(y) : "st(1)");
return x;
}
}
template<floating_point T>
inline constexpr T log(T x)
{
return detail::fyl2x<T>(x, numbers::ln2_v<T>);
}
template<floating_point T>
inline constexpr T log2(T x)
{
return detail::fyl2x<T>(x, 1.0);
}
template<floating_point T>
inline constexpr T log10(T x)
{
return detail::fyl2x<T>(x, numbers::lg2_v<T>);
}
template<floating_point T>
inline constexpr T logb(T x)
{
static_assert(FLT_RADIX == 2);
return log2<T>(x);
}
template<floating_point T>
inline constexpr T exp2(T x)
{
if (abs(x) <= (T)1.0)
{
asm("f2xm1" : "+t"(x));
return x + (T)1.0;
}
asm(
"fld1;"
"fld %%st(1);"
"fprem;"
"f2xm1;"
"faddp;"
"fscale;"
"fstp %%st(1);"
: "+t"(x)
);
return x;
}
template<floating_point T>
inline constexpr T exp(T x)
{
return exp2<T>(x * numbers::log2e_v<T>);
}
template<floating_point T>
inline constexpr T pow(T x, T y)
{
asm(
"fyl2x;"
"fld1;"
"fld %%st(1);"
"fprem;"
"f2xm1;"
"faddp;"
"fscale;"
: "+t"(x), "+u"(y)
);
return x;
}
template<floating_point T>
inline constexpr T scalbn(T x, int n)
{
asm("fscale" : "+t"(x) : "u"(static_cast<T>(n)));
return x;
}
template<floating_point T>
inline constexpr T ldexp(T x, int y)
{
const bool exp_sign = y < 0;
if (exp_sign)
y = -y;
T exp = (T)1.0;
T mult = (T)2.0;
while (y)
{
if (y & 1)
exp *= mult;
mult *= mult;
y >>= 1;
}
if (exp_sign)
exp = (T)1.0 / exp;
return x * exp;
}
template<floating_point T>
inline constexpr T sqrt(T x)
{
asm("fsqrt" : "+t"(x));
return x;
}
template<floating_point T>
inline constexpr T cbrt(T value)
{
if (value == 0.0)
return 0.0;
return pow<T>(value, 1.0 / 3.0);
}
template<floating_point T>
inline constexpr T sin(T x)
{
asm("fsin" : "+t"(x));
return x;
}
template<floating_point T>
inline constexpr T cos(T x)
{
asm("fcos" : "+t"(x));
return x;
}
template<floating_point T>
inline constexpr void sincos(T x, T& sin, T& cos)
{
asm("fsincos" : "=t"(cos), "=u"(sin) : "0"(x));
}
template<floating_point T>
inline constexpr T tan(T x)
{
T one, ret;
asm(
"fptan"
: "=t"(one), "=u"(ret)
: "0"(x)
);
return ret;
}
template<floating_point T>
inline constexpr T atan2(T y, T x)
{
asm(
"fpatan"
: "+t"(x)
: "u"(y)
: "st(1)"
);
return x;
}
template<floating_point T>
inline constexpr T atan(T x)
{
return atan2<T>(x, 1.0);
}
template<floating_point T>
inline constexpr T asin(T x)
{
if (x == (T)0.0)
return (T)0.0;
if (x == (T)1.0)
return numbers::pi_v<T> / (T)2.0;
if (x == (T)-1.0)
return -numbers::pi_v<T> / (T)2.0;
return (T)2.0 * atan<T>(x / (T(1.0) + sqrt<T>((T)1.0 - x * x)));
}
template<floating_point T>
inline constexpr T acos(T x)
{
if (x == (T)0.0)
return numbers::pi_v<T> / (T)2.0;
if (x == (T)1.0)
return (T)0.0;
if (x == (T)-1.0)
return numbers::pi_v<T>;
return (T)2.0 * atan<T>(sqrt<T>((T)1.0 - x * x) / ((T)1.0 + x));
}
template<floating_point T>
inline constexpr T sinh(T x)
{
return (exp<T>(x) - exp<T>(-x)) / (T)2.0;
}
template<floating_point T>
inline constexpr T cosh(T x)
{
return (exp<T>(x) + exp<T>(-x)) / (T)2.0;
}
template<floating_point T>
inline constexpr T tanh(T x)
{
const T exp_px = exp<T>(x);
const T exp_nx = exp<T>(-x);
return (exp_px - exp_nx) / (exp_px + exp_nx);
}
template<floating_point T>
inline constexpr T asinh(T x)
{
return log<T>(x + sqrt<T>(x * x + (T)1.0));
}
template<floating_point T>
inline constexpr T acosh(T x)
{
return log<T>(x + sqrt<T>(x * x - (T)1.0));
}
template<floating_point T>
inline constexpr T atanh(T x)
{
return (T)0.5 * log<T>(((T)1.0 + x) / ((T)1.0 - x));
}
template<floating_point T>
inline constexpr T hypot(T x, T y)
{
return sqrt<T>(x * x + y * y);
}
}

View File

@ -2,6 +2,8 @@
#include <BAN/Traits.h>
#include <stddef.h>
namespace BAN
{
@ -24,4 +26,4 @@ namespace BAN
return static_cast<T&&>(arg);
}
}
}

18
BAN/include/BAN/New.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#if defined(__is_kernel)
#include <kernel/Memory/kmalloc.h>
#else
#include <stdlib.h>
#endif
namespace BAN
{
#if defined(__is_kernel)
static constexpr void*(&allocator)(size_t) = kmalloc;
static constexpr void(&deallocator)(void*) = kfree;
#else
static constexpr void*(&allocator)(size_t) = malloc;
static constexpr void(&deallocator)(void*) = free;
#endif
}

28
BAN/include/BAN/Numbers.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include <BAN/Traits.h>
namespace BAN::numbers
{
template<floating_point T> inline constexpr T e_v = 2.71828182845904523536;
template<floating_point T> inline constexpr T log2e_v = 1.44269504088896340736;
template<floating_point T> inline constexpr T lge_v = 0.43429448190325182765;
template<floating_point T> inline constexpr T lg2_v = 0.30102999566398119521;
template<floating_point T> inline constexpr T ln2_v = 0.69314718055994530942;
template<floating_point T> inline constexpr T ln10_v = 2.30258509299404568402;
template<floating_point T> inline constexpr T pi_v = 3.14159265358979323846;
template<floating_point T> inline constexpr T sqrt2_v = 1.41421356237309504880;
template<floating_point T> inline constexpr T sqrt3_v = 1.73205080756887729353;
inline constexpr double e = e_v<double>;
inline constexpr double log2e = log2e_v<double>;
inline constexpr double lge = lge_v<double>;
inline constexpr double lg2 = lge_v<double>;
inline constexpr double ln2 = ln2_v<double>;
inline constexpr double ln10 = ln10_v<double>;
inline constexpr double pi = pi_v<double>;
inline constexpr double sqrt2 = sqrt2_v<double>;
inline constexpr double sqrt3 = sqrt3_v<double>;
}

204
BAN/include/BAN/Optional.h Normal file
View File

@ -0,0 +1,204 @@
#pragma once
#include <BAN/Assert.h>
#include <BAN/Move.h>
#include <BAN/PlacementNew.h>
#include <stdint.h>
namespace BAN
{
template<typename T>
class Optional
{
public:
constexpr Optional();
constexpr Optional(Optional&&);
constexpr Optional(const Optional&);
constexpr Optional(const T&);
constexpr Optional(T&&);
~Optional();
constexpr Optional& operator=(Optional&&);
constexpr Optional& operator=(const Optional&);
template<typename... Args>
constexpr Optional& emplace(Args&&...) requires is_constructible_v<T, Args...>;
constexpr T* operator->();
constexpr const T* operator->() const;
constexpr T& operator*();
constexpr const T& operator*() const;
constexpr bool has_value() const;
constexpr T release_value();
constexpr T& value();
constexpr const T& value() const;
constexpr T& value_or(T&);
constexpr const T& value_or(const T&) const;
constexpr void clear();
private:
alignas(T) uint8_t m_storage[sizeof(T)] {};
bool m_has_value { false };
};
template<typename T>
constexpr Optional<T>::Optional()
: m_has_value(false)
{}
template<typename T>
constexpr Optional<T>::Optional(Optional<T>&& other)
: m_has_value(other.has_value())
{
if (other.has_value())
new (m_storage) T(move(other.release_value()));
}
template<typename T>
constexpr Optional<T>::Optional(const Optional<T>& other)
: m_has_value(other.has_value())
{
if (other.has_value())
new (m_storage) T(other.value());
}
template<typename T>
constexpr Optional<T>::Optional(const T& value)
: m_has_value(true)
{
new (m_storage) T(value);
}
template<typename T>
constexpr Optional<T>::Optional(T&& value)
: m_has_value(true)
{
new (m_storage) T(move(value));
}
template<typename T>
Optional<T>::~Optional()
{
clear();
}
template<typename T>
constexpr Optional<T>& Optional<T>::operator=(Optional&& other)
{
clear();
m_has_value = other.has_value();
if (other.has_value())
new (m_storage) T(move(other.release_value()));
return *this;
}
template<typename T>
constexpr Optional<T>& Optional<T>::operator=(const Optional& other)
{
clear();
m_has_value = other.has_value();
if (other.has_value())
new (m_storage) T(other.value());
return *this;
}
template<typename T>
template<typename... Args>
constexpr Optional<T>& Optional<T>::emplace(Args&&... args) requires is_constructible_v<T, Args...>
{
clear();
m_has_value = true;
new (m_storage) T(forward<Args>(args)...);
return *this;
}
template<typename T>
constexpr T* Optional<T>::operator->()
{
ASSERT(has_value());
return &value();
}
template<typename T>
constexpr const T* Optional<T>::operator->() const
{
ASSERT(has_value());
return &value();
}
template<typename T>
constexpr T& Optional<T>::operator*()
{
ASSERT(has_value());
return value();
}
template<typename T>
constexpr const T& Optional<T>::operator*() const
{
ASSERT(has_value());
return value();
}
template<typename T>
constexpr bool Optional<T>::has_value() const
{
return m_has_value;
}
template<typename T>
constexpr T Optional<T>::release_value()
{
ASSERT(has_value());
T released_value = move(value());
value().~T();
m_has_value = false;
return move(released_value);
}
template<typename T>
constexpr T& Optional<T>::value()
{
ASSERT(has_value());
return *reinterpret_cast<T*>(&m_storage);
}
template<typename T>
constexpr const T& Optional<T>::value() const
{
ASSERT(has_value());
return *reinterpret_cast<const T*>(&m_storage);
}
template<typename T>
constexpr T& Optional<T>::value_or(T& empty)
{
if (!has_value())
return empty;
return value();
}
template<typename T>
constexpr const T& Optional<T>::value_or(const T& empty) const
{
if (!has_value())
return empty;
return value();
}
template<typename T>
constexpr void Optional<T>::clear()
{
if (m_has_value)
value().~T();
m_has_value = false;
}
}

View File

@ -0,0 +1,10 @@
#pragma once
#if __has_include(<new>)
#include <new>
#else
#include <stddef.h>
inline void* operator new(size_t, void* addr) { return addr; }
inline void* operator new[](size_t, void* addr) { return addr; }
#endif

View File

@ -1,9 +1,11 @@
#pragma once
#include <BAN/Errors.h>
#include <BAN/Iterators.h>
#include <BAN/Math.h>
#include <BAN/Memory.h>
#include <BAN/Move.h>
#include <BAN/New.h>
#include <BAN/PlacementNew.h>
namespace BAN
{
@ -14,6 +16,8 @@ namespace BAN
public:
using size_type = size_t;
using value_type = T;
using iterator = IteratorSimple<T, Queue>;
using const_iterator = ConstIteratorSimple<T, Queue>;
public:
Queue() = default;
@ -27,15 +31,21 @@ namespace BAN
ErrorOr<void> push(T&&);
ErrorOr<void> push(const T&);
template<typename... Args>
ErrorOr<void> emplace(Args&&...);
ErrorOr<void> emplace(Args&&...) requires is_constructible_v<T, Args...>;
ErrorOr<void> reserve(size_type);
ErrorOr<void> shrink_to_fit();
iterator begin() { return iterator(m_data); }
iterator end() { return iterator(m_data + m_size); }
const_iterator begin() const { return const_iterator(m_data); }
const_iterator end() const { return const_iterator(m_data + m_size); }
void pop();
void clear();
bool empty() const;
size_type capacity() const;
size_type size() const;
const T& front() const;
@ -121,7 +131,7 @@ namespace BAN
template<typename T>
template<typename... Args>
ErrorOr<void> Queue<T>::emplace(Args&&... args)
ErrorOr<void> Queue<T>::emplace(Args&&... args) requires is_constructible_v<T, Args...>
{
TRY(ensure_capacity(m_size + 1));
new (m_data + m_size) T(forward<Args>(args)...);
@ -177,6 +187,12 @@ namespace BAN
return m_size == 0;
}
template<typename T>
typename Queue<T>::size_type Queue<T>::capacity() const
{
return m_capacity;
}
template<typename T>
typename Queue<T>::size_type Queue<T>::size() const
{
@ -217,4 +233,4 @@ namespace BAN
return {};
}
}
}

View File

@ -1,27 +1,13 @@
#pragma once
#include <BAN/Atomic.h>
#include <BAN/Errors.h>
#include <BAN/Move.h>
#include <BAN/NoCopyMove.h>
#if defined(__is_kernel)
#include <kernel/kmalloc.h>
#else
#include <stdlib.h>
#endif
#include <stdint.h>
namespace BAN
{
#if defined(__is_kernel)
static constexpr void*(&allocator)(size_t) = kmalloc;
static constexpr void(&deallocator)(void*) = kfree;
#else
static constexpr void*(&allocator)(size_t) = malloc;
static constexpr void(&deallocator)(void*) = free;
#endif
template<typename T>
class RefCounted
@ -37,24 +23,36 @@ namespace BAN
void ref() const
{
ASSERT(m_ref_count > 0);
m_ref_count++;
uint32_t old = m_ref_count.fetch_add(1, MemoryOrder::memory_order_relaxed);
ASSERT(old > 0);
}
bool try_ref() const
{
uint32_t expected = m_ref_count.load(MemoryOrder::memory_order_relaxed);
for (;;)
{
if (expected == 0)
return false;
if (m_ref_count.compare_exchange(expected, expected + 1, MemoryOrder::memory_order_acquire))
return true;
}
}
void unref() const
{
ASSERT(m_ref_count > 0);
m_ref_count--;
if (m_ref_count == 0)
delete (const T*)this;
uint32_t old = m_ref_count.fetch_sub(1);
ASSERT(old > 0);
if (old == 1)
delete static_cast<const T*>(this);
}
protected:
RefCounted() = default;
~RefCounted() { ASSERT(m_ref_count == 0); }
virtual ~RefCounted() { ASSERT(m_ref_count == 0); }
private:
mutable uint32_t m_ref_count = 1;
mutable Atomic<uint32_t> m_ref_count = 1;
};
template<typename T>
@ -68,7 +66,6 @@ namespace BAN
if (m_pointer)
m_pointer->ref();
}
~RefPtr() { clear(); }
template<typename U>
@ -79,8 +76,9 @@ namespace BAN
return ptr;
}
// NOTE: don't use is_constructible_v<T, Args...> as RefPtr<T> is allowed with friends
template<typename... Args>
static ErrorOr<RefPtr> create(Args&&... args)
static ErrorOr<RefPtr> create(Args&&... args) requires requires(Args&&... args) { T(forward<Args>(args)...); }
{
T* pointer = new T(forward<Args>(args)...);
if (pointer == nullptr)
@ -90,6 +88,10 @@ namespace BAN
RefPtr(const RefPtr& other) { *this = other; }
RefPtr(RefPtr&& other) { *this = move(other); }
template<typename U>
RefPtr(const RefPtr<U>& other) { *this = other; }
template<typename U>
RefPtr(RefPtr<U>&& other) { *this = move(other); }
RefPtr& operator=(const RefPtr& other)
{
@ -108,6 +110,25 @@ namespace BAN
return *this;
}
template<typename U>
RefPtr& operator=(const RefPtr<U>& other)
{
clear();
m_pointer = other.m_pointer;
if (m_pointer)
m_pointer->ref();
return *this;
}
template<typename U>
RefPtr& operator=(RefPtr<U>&& other)
{
clear();
m_pointer = other.m_pointer;
other.m_pointer = nullptr;
return *this;
}
T* ptr() { ASSERT(!empty()); return m_pointer; }
const T* ptr() const { ASSERT(!empty()); return m_pointer; }
@ -117,8 +138,11 @@ namespace BAN
T* operator->() { return ptr(); }
const T* operator->() const { return ptr(); }
bool operator==(RefPtr other) const { return m_pointer == other.m_pointer; }
bool operator!=(RefPtr other) const { return m_pointer != other.m_pointer; }
bool empty() const { return m_pointer == nullptr; }
operator bool() const { return m_pointer; }
explicit operator bool() const { return m_pointer; }
void clear()
{
@ -129,9 +153,9 @@ namespace BAN
private:
T* m_pointer = nullptr;
template<typename U>
friend class RefPtr;
};
}
inline void* operator new(size_t, void* addr) { return addr; }
inline void* operator new[](size_t, void* addr) { return addr; }

View File

@ -13,10 +13,16 @@ namespace BAN
{ }
~ScopeGuard()
{
m_func();
if (m_enabled)
m_func();
}
void disable()
{
m_enabled = false;
}
private:
BAN::Function<void()> m_func;
bool m_enabled { true };
};
}
}

240
BAN/include/BAN/Sort.h Normal file
View File

@ -0,0 +1,240 @@
#pragma once
#include <BAN/Math.h>
#include <BAN/Swap.h>
#include <BAN/Traits.h>
#include <BAN/Vector.h>
namespace BAN::sort
{
template<typename It, typename Comp = less<typename It::value_type>>
void exchange_sort(It begin, It end, Comp comp = {})
{
for (It lhs = begin; lhs != end; ++lhs)
for (It rhs = next(lhs, 1); rhs != end; ++rhs)
if (!comp(*lhs, *rhs))
swap(*lhs, *rhs);
}
namespace detail
{
template<typename It, typename Comp>
It partition(It begin, It end, Comp comp)
{
It pivot = prev(end, 1);
It it1 = begin;
for (It it2 = begin; it2 != pivot; ++it2)
{
if (comp(*it2, *pivot))
{
swap(*it1, *it2);
++it1;
}
}
swap(*it1, *pivot);
return it1;
}
}
template<typename It, typename Comp = less<typename It::value_type>>
void quick_sort(It begin, It end, Comp comp = {})
{
if (distance(begin, end) <= 1)
return;
It mid = detail::partition(begin, end, comp);
quick_sort(begin, mid, comp);
quick_sort(++mid, end, comp);
}
template<typename It, typename Comp = less<typename It::value_type>>
void insertion_sort(It begin, It end, Comp comp = {})
{
if (distance(begin, end) <= 1)
return;
for (It it1 = next(begin, 1); it1 != end; ++it1)
{
typename It::value_type x = move(*it1);
It it2 = it1;
for (; it2 != begin && comp(x, *prev(it2, 1)); --it2)
*it2 = move(*prev(it2, 1));
*it2 = move(x);
}
}
namespace detail
{
template<typename It, typename Comp>
void push_heap(It begin, size_t hole_index, size_t top_index, typename It::value_type value, Comp comp)
{
size_t parent = (hole_index - 1) / 2;
while (hole_index > top_index && comp(*next(begin, parent), value))
{
*next(begin, hole_index) = move(*next(begin, parent));
hole_index = parent;
parent = (hole_index - 1) / 2;
}
*next(begin, hole_index) = move(value);
}
template<typename It, typename Comp>
void adjust_heap(It begin, size_t hole_index, size_t len, typename It::value_type value, Comp comp)
{
const size_t top_index = hole_index;
size_t child = hole_index;
while (child < (len - 1) / 2)
{
child = 2 * (child + 1);
if (comp(*next(begin, child), *next(begin, child - 1)))
child--;
*next(begin, hole_index) = move(*next(begin, child));
hole_index = child;
}
if (len % 2 == 0 && child == (len - 2) / 2)
{
child = 2 * (child + 1);
*next(begin, hole_index) = move(*next(begin, child - 1));
hole_index = child - 1;
}
push_heap(begin, hole_index, top_index, move(value), comp);
}
}
template<typename It, typename Comp = less<typename It::value_type>>
void make_heap(It begin, It end, Comp comp = {})
{
const size_t len = distance(begin, end);
if (len <= 1)
return;
size_t parent = (len - 2) / 2;
while (true)
{
detail::adjust_heap(begin, parent, len, move(*next(begin, parent)), comp);
if (parent == 0)
break;
parent--;
}
}
template<typename It, typename Comp = less<typename It::value_type>>
void sort_heap(It begin, It end, Comp comp = {})
{
const size_t len = distance(begin, end);
if (len <= 1)
return;
size_t last = len;
while (last > 1)
{
last--;
typename It::value_type x = move(*next(begin, last));
*next(begin, last) = move(*begin);
detail::adjust_heap(begin, 0, last, move(x), comp);
}
}
template<typename It, typename Comp = less<typename It::value_type>>
void heap_sort(It begin, It end, Comp comp = {})
{
make_heap(begin, end, comp);
sort_heap(begin, end, comp);
}
namespace detail
{
template<typename It, typename Comp>
void intro_sort_impl(It begin, It end, size_t max_depth, Comp comp)
{
if (distance(begin, end) <= 16)
return insertion_sort(begin, end, comp);
if (max_depth == 0)
return heap_sort(begin, end, comp);
It mid = detail::partition(begin, end, comp);
intro_sort_impl(begin, mid, max_depth - 1, comp);
intro_sort_impl(++mid, end, max_depth - 1, comp);
}
}
template<typename It, typename Comp = less<typename It::value_type>>
void intro_sort(It begin, It end, Comp comp = {})
{
const size_t len = distance(begin, end);
if (len <= 1)
return;
detail::intro_sort_impl(begin, end, 2 * Math::ilog2(len), comp);
}
namespace detail
{
template<unsigned_integral T>
consteval T lsb_index(T value)
{
for (T result = 0;; result++)
if (value & (1 << result))
return result;
}
}
template<typename It, size_t radix = 256>
requires is_unsigned_v<typename It::value_type> && (radix > 0 && (radix & (radix - 1)) == 0)
BAN::ErrorOr<void> radix_sort(It begin, It end)
{
using value_type = typename It::value_type;
const size_t len = distance(begin, end);
if (len <= 1)
return {};
Vector<value_type> temp;
TRY(temp.resize(len));
Vector<size_t> counts;
TRY(counts.resize(radix));
constexpr size_t mask = radix - 1;
constexpr size_t shift = detail::lsb_index(radix);
for (size_t s = 0; s < sizeof(value_type) * 8; s += shift)
{
for (auto& cnt : counts)
cnt = 0;
for (It it = begin; it != end; ++it)
counts[(*it >> s) & mask]++;
for (size_t i = 0; i < radix - 1; i++)
counts[i + 1] += counts[i];
for (It it = end; it != begin;)
{
--it;
temp[--counts[(*it >> s) & mask]] = *it;
}
for (size_t j = 0; j < temp.size(); j++)
*next(begin, j) = temp[j];
}
return {};
}
template<typename It, typename Comp = less<typename It::value_type>>
void sort(It begin, It end, Comp comp = {})
{
return intro_sort(begin, end, comp);
}
}

104
BAN/include/BAN/Span.h Normal file
View File

@ -0,0 +1,104 @@
#pragma once
#include <BAN/Assert.h>
#include <BAN/Iterators.h>
#include <stddef.h>
namespace BAN
{
template<typename T>
class Span
{
public:
using value_type = T;
using size_type = size_t;
using iterator = IteratorSimple<value_type, Span>;
using const_iterator = ConstIteratorSimple<value_type, Span>;
private:
template<typename S>
static inline constexpr bool can_init_from_v = is_same_v<value_type, const S> || is_same_v<value_type, S>;
public:
Span() = default;
Span(value_type* data, size_type size)
: m_data(data)
, m_size(size)
{ }
template<typename S>
Span(const Span<S>& other) requires can_init_from_v<S>
: m_data(other.m_data)
, m_size(other.m_size)
{ }
template<typename S>
Span(Span<S>&& other) requires can_init_from_v<S>
: m_data(other.m_data)
, m_size(other.m_size)
{
other.clear();
}
template<typename S>
Span& operator=(const Span<S>& other) requires can_init_from_v<S>
{
m_data = other.m_data;
m_size = other.m_size;
return *this;
}
template<typename S>
Span& operator=(Span<S>&& other) requires can_init_from_v<S>
{
m_data = other.m_data;
m_size = other.m_size;
return *this;
}
iterator begin() { return iterator(m_data); }
iterator end() { return iterator(m_data + m_size); }
const_iterator begin() const { return const_iterator(m_data); }
const_iterator end() const { return const_iterator(m_data + m_size); }
value_type& operator[](size_type index) const
{
ASSERT(index < m_size);
return m_data[index];
}
value_type* data() const
{
ASSERT(m_data);
return m_data;
}
bool empty() const { return m_size == 0; }
size_type size() const { return m_size; }
void clear()
{
m_data = nullptr;
m_size = 0;
}
Span slice(size_type start, size_type length = ~size_type(0)) const
{
ASSERT(m_data);
ASSERT(start <= m_size);
if (length == ~size_type(0))
length = m_size - start;
ASSERT(m_size - start >= length);
return Span(m_data + start, length);
}
Span<const value_type> as_const() const { return *this; }
private:
value_type* m_data = nullptr;
size_type m_size = 0;
friend class Span<const value_type>;
};
}

View File

@ -1,8 +1,11 @@
#pragma once
#include <BAN/ForwardList.h>
#include <BAN/Errors.h>
#include <BAN/Formatter.h>
#include <BAN/Hash.h>
#include <BAN/Iterators.h>
#include <BAN/New.h>
#include <BAN/StringView.h>
namespace BAN
{
@ -11,72 +14,319 @@ namespace BAN
{
public:
using size_type = size_t;
using iterator = IteratorSimple<char, String>;
using const_iterator = ConstIteratorSimple<char, String>;
static constexpr size_type sso_capacity = 15;
public:
String();
String(const String&);
String(String&&);
String(StringView);
~String();
String() {}
String(const String& other) { *this = other; }
String(String&& other) { *this = move(other); }
String(StringView other) { *this = other; }
~String() { clear(); }
template<typename... Args>
static String formatted(const char* format, const Args&... args);
static BAN::ErrorOr<String> formatted(const char* format, Args&&... args)
{
size_type length = 0;
BAN::Formatter::print([&](char) { length++; }, format, BAN::forward<Args>(args)...);
String& operator=(const String&);
String& operator=(String&&);
String& operator=(StringView);
String result;
TRY(result.reserve(length));
BAN::Formatter::print([&](char c){ MUST(result.push_back(c)); }, format, BAN::forward<Args>(args)...);
ErrorOr<void> push_back(char);
ErrorOr<void> insert(char, size_type);
ErrorOr<void> insert(StringView, size_type);
ErrorOr<void> append(StringView);
ErrorOr<void> append(const String&);
return result;
}
void pop_back();
void remove(size_type);
void erase(size_type, size_type);
String& operator=(const String& other)
{
clear();
MUST(ensure_capacity(other.size()));
memcpy(data(), other.data(), other.size() + 1);
m_size = other.size();
return *this;
}
void clear();
String& operator=(String&& other)
{
clear();
char operator[](size_type) const;
char& operator[](size_type);
if (other.has_sso())
memcpy(data(), other.data(), other.size() + 1);
else
{
m_storage.general_storage = other.m_storage.general_storage;
m_has_sso = false;
}
m_size = other.m_size;
bool operator==(const String&) const;
bool operator==(StringView) const;
bool operator==(const char*) const;
other.m_size = 0;
other.m_storage.sso_storage = SSOStorage();
other.m_has_sso = true;
ErrorOr<void> resize(size_type, char = '\0');
ErrorOr<void> reserve(size_type);
ErrorOr<void> shrink_to_fit();
return *this;
}
StringView sv() const;
String& operator=(StringView other)
{
clear();
MUST(ensure_capacity(other.size()));
memcpy(data(), other.data(), other.size());
m_size = other.size();
data()[m_size] = '\0';
return *this;
}
bool empty() const;
size_type size() const;
size_type capacity() const;
ErrorOr<void> push_back(char c)
{
TRY(ensure_capacity(m_size + 1));
data()[m_size] = c;
m_size++;
data()[m_size] = '\0';
return {};
}
const char* data() const;
ErrorOr<void> insert(char c, size_type index)
{
ASSERT(index <= m_size);
TRY(ensure_capacity(m_size + 1));
memmove(data() + index + 1, data() + index, m_size - index);
data()[index] = c;
m_size++;
data()[m_size] = '\0';
return {};
}
ErrorOr<void> insert(StringView str, size_type index)
{
ASSERT(index <= m_size);
TRY(ensure_capacity(m_size + str.size()));
memmove(data() + index + str.size(), data() + index, m_size - index);
memcpy(data() + index, str.data(), str.size());
m_size += str.size();
data()[m_size] = '\0';
return {};
}
ErrorOr<void> append(StringView str)
{
TRY(ensure_capacity(m_size + str.size()));
memcpy(data() + m_size, str.data(), str.size());
m_size += str.size();
data()[m_size] = '\0';
return {};
}
void pop_back()
{
ASSERT(m_size > 0);
m_size--;
data()[m_size] = '\0';
}
void remove(size_type index)
{
ASSERT(index < m_size);
memmove(data() + index, data() + index + 1, m_size - index);
m_size--;
data()[m_size] = '\0';
}
void clear()
{
if (!has_sso())
{
deallocator(m_storage.general_storage.data);
m_storage.sso_storage = SSOStorage();
m_has_sso = true;
}
m_size = 0;
data()[m_size] = '\0';
}
const_iterator begin() const { return const_iterator(data()); }
iterator begin() { return iterator(data()); }
const_iterator end() const { return const_iterator(data() + size()); }
iterator end() { return iterator(data() + size()); }
char front() const { ASSERT(m_size > 0); return data()[0]; }
char& front() { ASSERT(m_size > 0); return data()[0]; }
char back() const { ASSERT(m_size > 0); return data()[m_size - 1]; }
char& back() { ASSERT(m_size > 0); return data()[m_size - 1]; }
char operator[](size_type index) const { ASSERT(index < m_size); return data()[index]; }
char& operator[](size_type index) { ASSERT(index < m_size); return data()[index]; }
bool operator==(const String& str) const
{
if (size() != str.size())
return false;
for (size_type i = 0; i < m_size; i++)
if (data()[i] != str.data()[i])
return false;
return true;
}
bool operator==(StringView str) const
{
if (size() != str.size())
return false;
for (size_type i = 0; i < m_size; i++)
if (data()[i] != str.data()[i])
return false;
return true;
}
bool operator==(const char* cstr) const
{
for (size_type i = 0; i < m_size; i++)
if (data()[i] != cstr[i])
return false;
if (cstr[size()] != '\0')
return false;
return true;
}
ErrorOr<void> resize(size_type new_size, char init_c = '\0')
{
if (m_size == new_size)
return {};
// expanding
if (m_size < new_size)
{
TRY(ensure_capacity(new_size));
memset(data() + m_size, init_c, new_size - m_size);
m_size = new_size;
data()[m_size] = '\0';
return {};
}
m_size = new_size;
data()[m_size] = '\0';
return {};
}
ErrorOr<void> reserve(size_type new_size)
{
TRY(ensure_capacity(new_size));
return {};
}
ErrorOr<void> shrink_to_fit()
{
if (has_sso())
return {};
if (fits_in_sso())
{
char* data = m_storage.general_storage.data;
m_storage.sso_storage = SSOStorage();
m_has_sso = true;
memcpy(this->data(), data, m_size + 1);
deallocator(data);
return {};
}
GeneralStorage& storage = m_storage.general_storage;
if (storage.capacity == m_size)
return {};
char* new_data = (char*)allocator(m_size + 1);
if (new_data == nullptr)
return Error::from_errno(ENOMEM);
memcpy(new_data, storage.data, m_size);
deallocator(storage.data);
storage.capacity = m_size;
storage.data = new_data;
return {};
}
StringView sv() const { return StringView(data(), size()); }
bool empty() const { return m_size == 0; }
size_type size() const { return m_size; }
size_type capacity() const
{
if (has_sso())
return sso_capacity;
return m_storage.general_storage.capacity;
}
char* data()
{
if (has_sso())
return m_storage.sso_storage.data;
return m_storage.general_storage.data;
}
const char* data() const
{
if (has_sso())
return m_storage.sso_storage.data;
return m_storage.general_storage.data;
}
private:
ErrorOr<void> ensure_capacity(size_type);
ErrorOr<void> ensure_capacity(size_type new_size)
{
if (m_size >= new_size)
return {};
if (has_sso() && fits_in_sso(new_size))
return {};
ErrorOr<void> copy_impl(StringView);
void move_impl(String&&);
char* new_data = (char*)allocator(new_size + 1);
if (new_data == nullptr)
return Error::from_errno(ENOMEM);
if (m_size)
memcpy(new_data, data(), m_size + 1);
if (has_sso())
{
m_storage.general_storage = GeneralStorage();
m_has_sso = false;
}
else
deallocator(m_storage.general_storage.data);
auto& storage = m_storage.general_storage;
storage.capacity = new_size;
storage.data = new_data;
return {};
}
bool has_sso() const { return m_has_sso; }
bool fits_in_sso() const { return fits_in_sso(m_size); }
static bool fits_in_sso(size_type size) { return size < sso_capacity; }
private:
char* m_data = nullptr;
size_type m_capacity = 0;
size_type m_size = 0;
struct SSOStorage
{
char data[sso_capacity + 1] {};
};
struct GeneralStorage
{
size_type capacity { 0 };
char* data { nullptr };
};
private:
union {
SSOStorage sso_storage;
GeneralStorage general_storage;
} m_storage { .sso_storage = SSOStorage() };
size_type m_size : sizeof(size_type) * 8 - 1 { 0 };
size_type m_has_sso : 1 { true };
};
template<typename... Args>
String String::formatted(const char* format, const Args&... args)
{
String result;
BAN::Formatter::print([&](char c){ result.push_back(c); }, format, args...);
return result;
}
template<>
struct hash<String>
{

View File

@ -1,7 +1,11 @@
#pragma once
#include <BAN/ForwardList.h>
#include <BAN/Formatter.h>
#include <BAN/ForwardList.h>
#include <BAN/Hash.h>
#include <BAN/Iterators.h>
#include <BAN/Optional.h>
#include <BAN/Vector.h>
namespace BAN
{
@ -10,41 +14,232 @@ namespace BAN
{
public:
using size_type = size_t;
using const_iterator = ConstIteratorSimple<char, StringView>;
public:
StringView();
constexpr StringView() {}
constexpr StringView(const char* string, size_type len = -1)
{
if (len == size_type(-1))
len = strlen(string);
m_data = string;
m_size = len;
}
StringView(const String&);
StringView(const char*, size_type = -1);
char operator[](size_type) const;
constexpr const_iterator begin() const { return const_iterator(m_data); }
constexpr const_iterator end() const { return const_iterator(m_data + m_size); }
bool operator==(const String&) const;
bool operator==(StringView) const;
bool operator==(const char*) const;
constexpr char operator[](size_type index) const
{
ASSERT(index < m_size);
return m_data[index];
}
StringView substring(size_type, size_type = -1) const;
constexpr bool operator==(StringView other) const
{
if (m_size != other.m_size)
return false;
for (size_type i = 0; i < m_size; i++)
if (m_data[i] != other.m_data[i])
return false;
return true;
}
ErrorOr<Vector<StringView>> split(char, bool = false);
ErrorOr<Vector<StringView>> split(bool(*comp)(char), bool = false);
constexpr bool operator==(const char* other) const
{
for (size_type i = 0; i < m_size; i++)
if (m_data[i] != other[i])
return false;
return other[m_size] == '\0';
}
char back() const;
char front() const;
constexpr StringView substring(size_type index, size_type len = -1) const
{
ASSERT(index <= m_size);
if (len == size_type(-1))
len = m_size - index;
ASSERT(len <= m_size - index); // weird order to avoid overflow
StringView result;
result.m_data = m_data + index;
result.m_size = len;
return result;
}
size_type count(char) const;
ErrorOr<Vector<StringView>> split(char delim, bool allow_empties = false) const
{
size_type count = 0;
{
size_type start = 0;
for (size_type i = 0; i < m_size; i++)
{
if (m_data[i] == delim)
{
if (allow_empties || start != i)
count++;
start = i + 1;
}
}
if (start != m_size)
count++;
}
bool empty() const;
size_type size() const;
const char* data() const;
Vector<StringView> result;
TRY(result.reserve(count));
size_type start = 0;
for (size_type i = 0; i < m_size; i++)
{
if (m_data[i] == delim)
{
if (allow_empties || start != i)
TRY(result.push_back(this->substring(start, i - start)));
start = i + 1;
}
}
if (start < m_size || (start == m_size && allow_empties))
TRY(result.push_back(this->substring(start)));
return result;
}
ErrorOr<Vector<StringView>> split(bool(*comp)(char), bool allow_empties = false) const
{
size_type count = 0;
{
size_type start = 0;
for (size_type i = 0; i < m_size; i++)
{
if (comp(m_data[i]))
{
if (allow_empties || start != i)
count++;
start = i + 1;
}
}
if (start != m_size)
count++;
}
Vector<StringView> result;
TRY(result.reserve(count));
size_type start = 0;
for (size_type i = 0; i < m_size; i++)
{
if (comp(m_data[i]))
{
if (allow_empties || start != i)
TRY(result.push_back(this->substring(start, i - start)));
start = i + 1;
}
}
if (start < m_size || (start == m_size && allow_empties))
TRY(result.push_back(this->substring(start)));
return result;
}
constexpr char back() const
{
ASSERT(m_size > 0);
return m_data[m_size - 1];
}
constexpr char front() const
{
ASSERT(m_size > 0);
return m_data[0];
}
BAN::Optional<size_type> find(char ch) const
{
for (size_type i = 0; i < m_size; i++)
if (m_data[i] == ch)
return i;
return {};
}
BAN::Optional<size_type> find(bool(*comp)(char)) const
{
for (size_type i = 0; i < m_size; i++)
if (comp(m_data[i]))
return i;
return {};
}
BAN::Optional<size_type> rfind(char ch) const
{
for (size_type i = m_size; i > 0; i--)
if (m_data[i - 1] == ch)
return i - 1;
return {};
}
BAN::Optional<size_type> rfind(bool(*comp)(char)) const
{
for (size_type i = m_size; i > 0; i--)
if (comp(m_data[i - 1]))
return i - 1;
return {};
}
constexpr bool starts_with(BAN::StringView target) const
{
if (target.size() > m_size)
return false;
for (size_type i = 0; i < target.size(); i++)
if (m_data[i] != target[i])
return false;
return true;
}
constexpr bool contains(char ch) const
{
for (size_type i = 0; i < m_size; i++)
if (m_data[i] == ch)
return true;
return false;
}
constexpr size_type count(char ch) const
{
size_type result = 0;
for (size_type i = 0; i < m_size; i++)
if (m_data[i] == ch)
result++;
return result;
}
constexpr bool empty() const { return m_size == 0; }
constexpr size_type size() const { return m_size; }
constexpr const char* data() const { return m_data; }
private:
const char* m_data = nullptr;
size_type m_size = 0;
};
template<>
struct hash<StringView>
{
hash_t operator()(StringView string) const
{
constexpr hash_t FNV_offset_basis = 0x811c9dc5;
constexpr hash_t FNV_prime = 0x01000193;
hash_t hash = FNV_offset_basis;
for (StringView::size_type i = 0; i < string.size(); i++)
{
hash *= FNV_prime;
hash ^= (uint8_t)string[i];
}
return hash;
}
};
}
inline BAN::StringView operator""sv(const char* str, BAN::StringView::size_type len) { return BAN::StringView(str, len); }
inline constexpr BAN::StringView operator""_sv(const char* str, BAN::StringView::size_type len) { return BAN::StringView(str, len); }
namespace BAN::Formatter
{

16
BAN/include/BAN/Swap.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <BAN/Move.h>
namespace BAN
{
template<typename T>
void swap(T& lhs, T& rhs)
{
T tmp = move(lhs);
lhs = move(rhs);
rhs = move(tmp);
}
}

View File

@ -9,15 +9,18 @@ namespace BAN
struct Time
{
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t week_day;
uint8_t day;
uint32_t year;
uint8_t month;
int year;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
uint8_t week_day;
};
uint64_t to_unix_time(const BAN::Time&);
BAN::Time from_unix_time(uint64_t);
}
namespace BAN::Formatter
@ -31,4 +34,4 @@ namespace BAN::Formatter
print(putc, "{} {} {} {2}:{2}:{2} GMT+0 {4}", week_days[time.week_day], months[time.month], time.day, time.hour, time.minute, time.second, time.year);
}
}
}

View File

@ -2,11 +2,11 @@
namespace BAN
{
template<typename T> struct remove_refenrece { using type = T; };
template<typename T> struct remove_refenrece<T&> { using type = T; };
template<typename T> struct remove_refenrece<T&&> { using type = T; };
template<typename T> using remove_reference_t = typename remove_refenrece<T>::type;
template<typename T> struct remove_reference { using type = T; };
template<typename T> struct remove_reference<T&> { using type = T; };
template<typename T> struct remove_reference<T&&> { using type = T; };
template<typename T> using remove_reference_t = typename remove_reference<T>::type;
template<typename T> struct remove_const { using type = T; };
template<typename T> struct remove_const<const T> { using type = T; };
@ -30,16 +30,36 @@ namespace BAN
template<typename T> struct maybe_const<true, T> { using type = const T; };
template<bool B, typename T> using maybe_const_t = typename maybe_const<B, T>::type;
struct true_type { static constexpr bool value = true; };
struct false_type { static constexpr bool value = false; };
template<bool B, typename T1, typename T2> struct either_or { using type = T2; };
template<typename T1, typename T2> struct either_or<true, T1, T2> { using type = T1; };
template<bool B, typename T1, typename T2> using either_or_t = typename either_or<B, T1, T2>::type;
template<typename T, T V> struct integral_constant { static constexpr T value = V; };
template<typename T, T V > inline constexpr T integral_constant_v = integral_constant<T, V>::value;
using true_type = integral_constant<bool, true>;
using false_type = integral_constant<bool, false>;
template<typename T, typename S> struct is_same : false_type {};
template<typename T> struct is_same<T, T> : true_type {};
template<typename T, typename S> inline constexpr bool is_same_v = is_same<T, S>::value;
template<typename T, typename S> concept same_as = BAN::is_same_v<T, S>;
template<typename T> struct is_lvalue_reference : false_type {};
template<typename T> struct is_lvalue_reference<T&> : true_type {};
template<typename T> inline constexpr bool is_lvalue_reference_v = is_lvalue_reference<T>::value;
template<typename T> concept lvalue_reference = is_lvalue_reference_v<T>;
template<typename T, typename... Args> struct is_constructible { static constexpr bool value = __is_constructible(T, Args...); };
template<typename T, typename... Args> inline constexpr bool is_constructible_v = is_constructible<T, Args...>::value;
template<typename T> struct is_default_constructible { static constexpr bool value = is_constructible_v<T>; };
template<typename T> inline constexpr bool is_default_constructible_v = is_default_constructible<T>::value;
template<typename T> struct is_copy_constructible { static constexpr bool value = is_constructible_v<T, const T&>; };
template<typename T> inline constexpr bool is_copy_constructible_v = is_copy_constructible<T>::value;
template<typename T> struct is_move_constructible { static constexpr bool value = is_constructible_v<T, T&&>; };
template<typename T> inline constexpr bool is_move_constructible_v = is_move_constructible<T>::value;
template<typename T> struct is_integral { static constexpr bool value = requires (T t, T* p, void (*f)(T)) { reinterpret_cast<T>(t); f(0); p + t; }; };
template<typename T> inline constexpr bool is_integral_v = is_integral<T>::value;
@ -60,8 +80,67 @@ namespace BAN
template<typename T> inline constexpr bool is_pointer_v = is_pointer<T>::value;
template<typename T> concept pointer = is_pointer_v<T>;
template<typename T> struct is_const : false_type {};
template<typename T> struct is_const<const T> : true_type {};
template<typename T> inline constexpr bool is_const_v = is_const<T>::value;
template<typename T> struct is_arithmetic { static constexpr bool value = is_integral_v<T> || is_floating_point_v<T>; };
template<typename T> inline constexpr bool is_arithmetic_v = is_arithmetic<T>::value;
template<typename Base, typename Derived> struct is_base_of { static constexpr bool value = __is_base_of(Base, Derived); };
template<typename Base, typename Derived> inline constexpr bool is_base_of_v = is_base_of<Base, Derived>::value;
template<typename T> struct is_pod { static constexpr bool value = __is_pod(T); };
template<typename T> inline constexpr bool is_pod_v = is_pod<T>::value;
namespace detail
{
template<typename T, bool = is_arithmetic_v<T>> struct is_signed { static constexpr bool value = T(-1) < T(0); };
template<typename T> struct is_signed<T, false> : false_type {};
template<typename T, bool = is_arithmetic_v<T>> struct is_unsigned { static constexpr bool value = T(0) < T(-1); };
template<typename T> struct is_unsigned<T, false> : false_type {};
}
template<typename T> struct is_signed : detail::is_signed<T> {};
template<typename T> inline constexpr bool is_signed_v = is_signed<T>::value;
template<typename T> concept signed_integral = is_signed_v<T> && is_integral_v<T>;
template<typename T> struct is_unsigned : detail::is_unsigned<T> {};
template<typename T> inline constexpr bool is_unsigned_v = is_unsigned<T>::value;
template<typename T> concept unsigned_integral = is_unsigned_v<T> && is_integral_v<T>;
#define __BAN_TRAITS_MAKE_UNSIGNED_CV(__type) \
template<> struct make_unsigned<__type> { using type = unsigned __type; }; \
template<> struct make_unsigned<const __type> { using type = unsigned const __type; }; \
template<> struct make_unsigned<volatile __type> { using type = unsigned volatile __type; }; \
template<> struct make_unsigned<const volatile __type> { using type = unsigned const volatile __type; };
template<typename T> requires is_arithmetic_v<T> struct make_unsigned { using type = T; };
__BAN_TRAITS_MAKE_UNSIGNED_CV(char)
__BAN_TRAITS_MAKE_UNSIGNED_CV(short)
__BAN_TRAITS_MAKE_UNSIGNED_CV(int)
__BAN_TRAITS_MAKE_UNSIGNED_CV(long)
__BAN_TRAITS_MAKE_UNSIGNED_CV(long long)
template<typename T> using make_unsigned_t = typename make_unsigned<T>::type;
#undef __BAN_TRAITS_MAKE_UNSIGNED_CV
#define __BAN_TRAITS_MAKE_SIGNED_CV(__type) \
template<> struct make_signed<unsigned __type> { using type = __type; }; \
template<> struct make_signed<unsigned const __type> { using type = const __type; }; \
template<> struct make_signed<unsigned volatile __type> { using type = volatile __type; }; \
template<> struct make_signed<unsigned const volatile __type> { using type = const volatile __type; };
template<typename T> requires is_arithmetic_v<T> struct make_signed { using type = T; };
__BAN_TRAITS_MAKE_SIGNED_CV(char)
__BAN_TRAITS_MAKE_SIGNED_CV(short)
__BAN_TRAITS_MAKE_SIGNED_CV(int)
__BAN_TRAITS_MAKE_SIGNED_CV(long)
__BAN_TRAITS_MAKE_SIGNED_CV(long long)
template<typename T> using make_signed_t = typename make_signed<T>::type;
#undef __BAN_TRAITS_MAKE_SIGNED_CV
template<typename T> struct less { constexpr bool operator()(const T& lhs, const T& rhs) const { return lhs < rhs; } };
template<typename T> struct equal { constexpr bool operator()(const T& lhs, const T& rhs) const { return lhs == rhs; } };
template<typename T> struct greater { constexpr bool operator()(const T& lhs, const T& rhs) const { return lhs > rhs; } };
}
}

View File

@ -3,26 +3,80 @@
#include <stddef.h>
#include <stdint.h>
namespace BAN
namespace BAN::UTF8
{
static constexpr uint16_t utf8_to_codepoint(uint8_t* bytes, size_t count)
static constexpr uint32_t invalid = 0xFFFFFFFF;
constexpr uint32_t byte_length(uint8_t first_byte)
{
if (count > 3)
return 0xFFFF;
for (size_t i = 1; i < count; i++)
if ((bytes[i] & 0xC0) != 0x80)
return 0xFFFF;
switch (count)
{
case 1: return bytes[0];
case 2: return ((bytes[0] & 0x1F) << 6) | (bytes[1] & 0x3F);
case 3: return ((bytes[0] & 0x1F) << 12) | ((bytes[1] & 0x3F) << 6) | (bytes[2] & 0x3F);
}
return 0xFFFF;
if ((first_byte & 0x80) == 0x00)
return 1;
if ((first_byte & 0xE0) == 0xC0)
return 2;
if ((first_byte & 0xF0) == 0xE0)
return 3;
if ((first_byte & 0xF8) == 0xF0)
return 4;
return 0;
}
}
template<typename T> requires (sizeof(T) == 1)
constexpr uint32_t to_codepoint(const T* bytes)
{
uint32_t length = byte_length(bytes[0]);
for (uint32_t i = 1; i < length; i++)
if (((uint8_t)bytes[i] & 0xC0) != 0x80)
return UTF8::invalid;
switch (length)
{
case 1: return (((uint8_t)bytes[0] & 0x80) != 0x00) ? UTF8::invalid : (uint8_t)bytes[0];
case 2: return (((uint8_t)bytes[0] & 0xE0) != 0xC0) ? UTF8::invalid : (((uint8_t)bytes[0] & 0x1F) << 6) | ((uint8_t)bytes[1] & 0x3F);
case 3: return (((uint8_t)bytes[0] & 0xF0) != 0xE0) ? UTF8::invalid : (((uint8_t)bytes[0] & 0x0F) << 12) | (((uint8_t)bytes[1] & 0x3F) << 6) | ((uint8_t)bytes[2] & 0x3F);
case 4: return (((uint8_t)bytes[0] & 0xF8) != 0xF0) ? UTF8::invalid : (((uint8_t)bytes[0] & 0x07) << 18) | (((uint8_t)bytes[1] & 0x3F) << 12) | (((uint8_t)bytes[2] & 0x3F) << 6) | ((uint8_t)bytes[3] & 0x3F);
}
return UTF8::invalid;
}
template<typename T>
constexpr bool from_codepoints(const T* codepoints, size_t count, char* out)
{
uint8_t* ptr = (uint8_t*)out;
for (size_t i = 0; i < count; i++)
{
if (codepoints[i] < 0x80)
{
*ptr++ = codepoints[i];
}
else if (codepoints[i] < 0x800)
{
*ptr++ = 0xC0 | ((codepoints[i] >> 6) & 0x1F);
*ptr++ = 0x80 | ((codepoints[i] >> 0) & 0x3F);
}
else if (codepoints[i] < 0x10000)
{
*ptr++ = 0xE0 | ((codepoints[i] >> 12) & 0x0F);
*ptr++ = 0x80 | ((codepoints[i] >> 6) & 0x3F);
*ptr++ = 0x80 | ((codepoints[i] >> 0) & 0x3F);
}
else if (codepoints[i] < 0x110000)
{
*ptr++ = 0xF0 | ((codepoints[i] >> 18) & 0x07);
*ptr++ = 0x80 | ((codepoints[i] >> 12) & 0x3F);
*ptr++ = 0x80 | ((codepoints[i] >> 6) & 0x3F);
*ptr++ = 0x80 | ((codepoints[i] >> 0) & 0x3F);
}
else
{
return false;
}
}
return true;
}
}

99
BAN/include/BAN/UniqPtr.h Normal file
View File

@ -0,0 +1,99 @@
#pragma once
#include <BAN/Errors.h>
#include <BAN/NoCopyMove.h>
namespace BAN
{
template<typename T>
class UniqPtr
{
BAN_NON_COPYABLE(UniqPtr);
public:
UniqPtr() = default;
template<typename U>
UniqPtr(UniqPtr<U>&& other)
{
m_pointer = other.m_pointer;
other.m_pointer = nullptr;
}
~UniqPtr()
{
clear();
}
static UniqPtr adopt(T* pointer)
{
UniqPtr uniq;
uniq.m_pointer = pointer;
return uniq;
}
// NOTE: don't use is_constructible_v<T, Args...> as UniqPtr<T> is allowed with friends
template<typename... Args>
static BAN::ErrorOr<UniqPtr> create(Args&&... args) requires requires(Args&&... args) { T(forward<Args>(args)...); }
{
UniqPtr uniq;
uniq.m_pointer = new T(BAN::forward<Args>(args)...);
if (uniq.m_pointer == nullptr)
return BAN::Error::from_errno(ENOMEM);
return uniq;
}
template<typename U>
UniqPtr& operator=(UniqPtr<U>&& other)
{
clear();
m_pointer = other.m_pointer;
other.m_pointer = nullptr;
return *this;
}
T& operator*()
{
ASSERT(m_pointer);
return *m_pointer;
}
const T& operator*() const
{
ASSERT(m_pointer);
return *m_pointer;
}
T* operator->()
{
ASSERT(m_pointer);
return m_pointer;
}
const T* operator->() const
{
ASSERT(m_pointer);
return m_pointer;
}
T* ptr() { return m_pointer; }
const T* ptr() const { return m_pointer; }
void clear()
{
if (m_pointer)
delete m_pointer;
m_pointer = nullptr;
}
operator bool() const { return m_pointer != nullptr; }
private:
T* m_pointer = nullptr;
template<typename U>
friend class UniqPtr;
};
}

View File

@ -3,145 +3,311 @@
#include <BAN/Assert.h>
#include <BAN/Math.h>
#include <BAN/Move.h>
#include <BAN/PlacementNew.h>
#include <string.h>
namespace BAN
{
template<typename T1, typename T2>
namespace detail
{
template<typename T>
constexpr size_t size_ref_as_ptr() { return is_lvalue_reference_v<T> ? sizeof(remove_reference_t<T>*) : sizeof(T); }
template<typename T>
constexpr size_t align_ref_as_ptr() { return is_lvalue_reference_v<T> ? alignof(remove_reference_t<T>*) : alignof(T); }
template<typename T>
constexpr size_t max_size_ref_as_ptr() { return size_ref_as_ptr<T>(); }
template<typename T0, typename T1, typename... Ts>
constexpr size_t max_size_ref_as_ptr() { return size_ref_as_ptr<T0>() > size_ref_as_ptr<T1>() ? max_size_ref_as_ptr<T0, Ts...>() : max_size_ref_as_ptr<T1, Ts...>(); }
template<typename T>
constexpr size_t max_align_ref_as_ptr() { return align_ref_as_ptr<T>(); }
template<typename T0, typename T1, typename... Ts>
constexpr size_t max_align_ref_as_ptr() { return align_ref_as_ptr<T0>() > align_ref_as_ptr<T1>() ? max_align_ref_as_ptr<T0, Ts...>() : max_align_ref_as_ptr<T1, Ts...>(); }
template<typename T, typename T0, typename... Ts>
constexpr size_t index()
{
if constexpr(is_same_v<T, T0>)
return 0;
else if constexpr(sizeof...(Ts) == 0)
return 1;
else
return index<T, Ts...>() + 1;
}
template<typename T, typename... Ts>
void destruct(size_t index, uint8_t* data)
{
if (index == 0)
{
if constexpr(!is_lvalue_reference_v<T>)
reinterpret_cast<T*>(data)->~T();
}
else if constexpr(sizeof...(Ts) > 0)
destruct<Ts...>(index - 1, data);
else
ASSERT_NOT_REACHED();
}
template<typename T, typename... Ts>
void move_construct(size_t index, uint8_t* source, uint8_t* target)
{
if (index == 0)
if constexpr(!is_lvalue_reference_v<T>)
new (target) T(move(*reinterpret_cast<T*>(source)));
else
memcpy(target, source, sizeof(remove_reference_t<T>*));
else if constexpr(sizeof...(Ts) > 0)
move_construct<Ts...>(index - 1, source, target);
else
ASSERT_NOT_REACHED();
}
template<typename T, typename... Ts>
void copy_construct(size_t index, const uint8_t* source, uint8_t* target)
{
if (index == 0)
if constexpr(!is_lvalue_reference_v<T>)
new (target) T(*reinterpret_cast<const T*>(source));
else
memcpy(target, source, sizeof(remove_reference_t<T>*));
else if constexpr(sizeof...(Ts) > 0)
copy_construct<Ts...>(index - 1, source, target);
else
ASSERT_NOT_REACHED();
}
template<typename T, typename... Ts>
void move_assign(size_t index, uint8_t* source, uint8_t* target)
{
if (index == 0)
if constexpr(!is_lvalue_reference_v<T>)
*reinterpret_cast<T*>(target) = move(*reinterpret_cast<T*>(source));
else
memcpy(target, source, sizeof(remove_reference_t<T>*));
else if constexpr(sizeof...(Ts) > 0)
move_assign<Ts...>(index - 1, source, target);
else
ASSERT_NOT_REACHED();
}
template<typename T, typename... Ts>
void copy_assign(size_t index, const uint8_t* source, uint8_t* target)
{
if (index == 0)
if constexpr(!is_lvalue_reference_v<T>)
*reinterpret_cast<T*>(target) = *reinterpret_cast<const T*>(source);
else
memcpy(target, source, sizeof(remove_reference_t<T>*));
else if constexpr(sizeof...(Ts) > 0)
copy_assign<Ts...>(index - 1, source, target);
else
ASSERT_NOT_REACHED();
}
}
template<typename... Ts>
requires (!is_const_v<Ts> && ...)
class Variant
{
public:
static_assert(!is_same_v<T1, T2>);
private:
template<typename T>
static constexpr bool can_have() { return detail::index<T, Ts...>() != invalid_index(); }
static constexpr size_t invalid_index() { return sizeof...(Ts); }
public:
Variant() = default;
Variant(const T1& value) { set(value); }
Variant(T1&& value) { set(move(value)); }
Variant(const T2& value) { set(value); }
Variant(T2&& value) { set(move(value)); }
Variant(Variant&& other)
: m_index(other.m_index)
{
detail::move_construct<Ts...>(other.m_index, other.m_storage, m_storage);
other.clear();
}
Variant(const Variant<T1, T2>& other) { *this = other; }
Variant(Variant<T1, T2>&& other) { *this = move(other); }
Variant(const Variant& other)
: m_index(other.m_index)
{
detail::copy_construct<Ts...>(other.m_index, other.m_storage, m_storage);
}
~Variant() { clear(); }
template<typename T>
Variant(T&& value) requires (can_have<T>() && !is_lvalue_reference_v<T>)
: m_index(detail::index<T, Ts...>())
{
new (m_storage) T(move(value));
}
Variant<T1, T2>& operator=(const Variant<T1, T2>& other);
Variant<T1, T2>& operator=(Variant<T1, T2>&& other);
template<typename T>
Variant(const T& value) requires (can_have<T>() && !is_lvalue_reference_v<T>)
: m_index(detail::index<T, Ts...>())
{
new (m_storage) T(value);
}
template<typename U>
bool is() const;
~Variant()
{
clear();
}
template<typename U>
void set(U&&);
template<typename U>
void set(const U& value) { set(move(U(value))); }
Variant& operator=(Variant&& other)
{
if (m_index == other.m_index)
detail::move_assign<Ts...>(m_index, other.m_storage, m_storage);
else
{
clear();
detail::move_construct<Ts...>(other.m_index, other.m_storage, m_storage);
m_index = other.m_index;
}
other.clear();
return *this;
}
template<typename U>
const U& get() const;
template<typename U>
U& get();
Variant& operator=(const Variant& other)
{
if (m_index == other.m_index)
detail::copy_assign<Ts...>(m_index, other.m_storage, m_storage);
else
{
clear();
detail::copy_construct<Ts...>(other.m_index, other.m_storage, m_storage);
m_index = other.m_index;
}
return *this;
}
void clear();
template<typename T>
Variant& operator=(T&& value) requires (can_have<T>() && !is_lvalue_reference_v<T>)
{
if (size_t index = detail::index<T, Ts...>(); index == m_index)
get<T>() = move(value);
else
{
clear();
new (m_storage) T(move(value));
m_index = index;
}
return *this;
}
template<typename T>
Variant& operator=(const T& value) requires (can_have<T>() && !is_lvalue_reference_v<T>)
{
if (size_t index = detail::index<T, Ts...>(); index == m_index)
get<T>() = value;
else
{
clear();
new (m_storage) T(value);
m_index = index;
}
return *this;
}
template<typename T>
bool has() const requires (can_have<T>())
{
return m_index == detail::index<T, Ts...>();
}
template<typename T, typename... Args>
void emplace(Args&&... args) requires (can_have<T>() && is_constructible_v<T, Args...>)
{
clear();
m_index = detail::index<T, Ts...>();
new (m_storage) T(BAN::forward<Args>(args)...);
}
template<typename T>
void set(T&& value) requires (can_have<T>() && !is_lvalue_reference_v<T>)
{
if (has<T>())
get<T>() = move(value);
else
{
clear();
m_index = detail::index<T, Ts...>();
new (m_storage) T(move(value));
}
}
template<typename T>
void set(const T& value) requires (can_have<T>() && !is_lvalue_reference_v<T>)
{
if (has<T>())
get<T>() = value;
else
{
clear();
m_index = detail::index<T, Ts...>();
new (m_storage) T(value);
}
}
template<typename T>
void set(T value) requires (can_have<T>() && is_lvalue_reference_v<T>)
{
clear();
m_index = detail::index<T, Ts...>();
*reinterpret_cast<remove_reference_t<T>**>(m_storage) = &value;
}
template<typename T>
T& get() requires (can_have<T>() && !is_lvalue_reference_v<T>)
{
ASSERT(has<T>());
return *reinterpret_cast<T*>(m_storage);
}
template<typename T>
const T& get() const requires (can_have<T>() && !is_lvalue_reference_v<T>)
{
ASSERT(has<T>());
return *reinterpret_cast<const T*>(m_storage);
}
template<typename T>
T get() requires (can_have<T>() && is_lvalue_reference_v<T>)
{
ASSERT(has<T>());
return **reinterpret_cast<remove_reference_t<T>**>(m_storage);
}
template<typename T>
const T get() const requires (can_have<T>() && is_lvalue_reference_v<T>)
{
ASSERT(has<T>());
return **reinterpret_cast<const remove_reference_t<T>**>(m_storage);
}
bool has_value() const
{
return m_index != invalid_index();
}
explicit operator bool() const
{
return has_value();
}
void clear()
{
if (m_index != invalid_index())
{
detail::destruct<Ts...>(m_index, m_storage);
m_index = invalid_index();
}
}
private:
static constexpr uint32_t m_size = Math::max(sizeof(T1), sizeof(T2));
uint8_t m_storage[m_size] = {};
uint32_t m_index = 0;
alignas(detail::max_align_ref_as_ptr<Ts...>()) uint8_t m_storage[detail::max_size_ref_as_ptr<Ts...>()] {};
size_t m_index { invalid_index() };
};
template<typename T1, typename T2>
Variant<T1, T2>& Variant<T1, T2>::operator=(const Variant<T1, T2>& other)
{
clear();
if (other.is<T1>())
set(other.get<T1>());
if (other.is<T2>())
set(other.get<T2>());
return *this;
}
template<typename T1, typename T2>
Variant<T1, T2>& Variant<T1, T2>::operator=(Variant<T1, T2>&& other)
{
clear();
if (other.is<T1>())
set(move(other.get<T1>()));
if (other.is<T2>())
set(move(other.get<T2>()));
other.clear();
return *this;
}
template<typename T1, typename T2>
template<typename U>
bool Variant<T1, T2>::is() const
{
if constexpr(is_same_v<T1, U>)
return m_index == 1;
if constexpr(is_same_v<T2, U>)
return m_index == 2;
return false;
}
template<typename T1, typename T2>
template<typename U>
void Variant<T1, T2>::set(U&& value)
{
static_assert(is_same_v<T1, U> || is_same_v<T2, U>);
clear();
if constexpr(is_same_v<T1, U>)
{
new (m_storage) T1(move(value));
m_index = 1;
}
if constexpr(is_same_v<T2, U>)
{
new (m_storage) T2(move(value));
m_index = 2;
}
}
template<typename T1, typename T2>
template<typename U>
const U& Variant<T1, T2>::get() const
{
static_assert(is_same_v<T1, U> || is_same_v<T2, U>);
if constexpr(is_same_v<T1, U>)
{
ASSERT(m_index == 1);
return *(T1*)m_storage;
}
if constexpr(is_same_v<T2, U>)
{
ASSERT(m_index == 2);
return *(T2*)m_storage;
}
}
template<typename T1, typename T2>
template<typename U>
U& Variant<T1, T2>::get()
{
static_assert(is_same_v<T1, U> || is_same_v<T2, U>);
if constexpr(is_same_v<T1, U>)
{
ASSERT(m_index == 1);
return *(T1*)m_storage;
}
if constexpr(is_same_v<T2, U>)
{
ASSERT(m_index == 2);
return *(T2*)m_storage;
}
}
template<typename T1, typename T2>
void Variant<T1, T2>::clear()
{
if (is<T1>()) ((T1*)m_storage)->~T1();
if (is<T2>()) ((T2*)m_storage)->~T2();
m_index = 0;
}
}
}

View File

@ -1,16 +1,17 @@
#pragma once
#include <BAN/Errors.h>
#include <BAN/Iterators.h>
#include <BAN/Math.h>
#include <BAN/Memory.h>
#include <BAN/Move.h>
#include <BAN/New.h>
#include <BAN/PlacementNew.h>
#include <BAN/Span.h>
#include <BAN/Swap.h>
namespace BAN
{
template<typename T, bool CONST>
class VectorIterator;
// T must be move assignable, move constructable (and copy constructable for some functions)
template<typename T>
class Vector
@ -18,8 +19,8 @@ namespace BAN
public:
using size_type = size_t;
using value_type = T;
using iterator = VectorIterator<T, false>;
using const_iterator = VectorIterator<T, true>;
using iterator = IteratorSimple<T, Vector>;
using const_iterator = ConstIteratorSimple<T, Vector>;
public:
Vector() = default;
@ -34,16 +35,16 @@ namespace BAN
ErrorOr<void> push_back(T&&);
ErrorOr<void> push_back(const T&);
template<typename... Args>
ErrorOr<void> emplace_back(Args&&...);
ErrorOr<void> emplace_back(Args&&...) requires is_constructible_v<T, Args...>;
template<typename... Args>
ErrorOr<void> emplace(size_type, Args&&...);
ErrorOr<void> emplace(size_type, Args&&...) requires is_constructible_v<T, Args...>;
ErrorOr<void> insert(size_type, T&&);
ErrorOr<void> insert(size_type, const T&);
iterator begin() { return iterator (m_data); }
const_iterator begin() const { return const_iterator(m_data); }
iterator end() { return iterator (m_data + m_size); }
const_iterator end() const { return const_iterator(m_data + m_size); }
iterator begin() { return iterator(m_data); }
iterator end() { return iterator(m_data + m_size); }
const_iterator begin() const { return const_iterator(m_data); }
const_iterator end() const { return const_iterator(m_data + m_size); }
void pop_back();
void remove(size_type);
@ -54,6 +55,9 @@ namespace BAN
bool contains(const T&) const;
Span<T> span() { return Span(m_data, m_size); }
const Span<T> span() const { return Span(m_data, m_size); }
const T& operator[](size_type) const;
T& operator[](size_type);
@ -62,7 +66,10 @@ namespace BAN
const T& front() const;
T& front();
ErrorOr<void> resize(size_type);
void reverse();
ErrorOr<void> resize(size_type) requires is_default_constructible_v<T>;
ErrorOr<void> resize(size_type, const T&) requires is_copy_constructible_v<T>;
ErrorOr<void> reserve(size_type);
ErrorOr<void> shrink_to_fit();
@ -76,54 +83,9 @@ namespace BAN
private:
T* m_data = nullptr;
size_type m_capacity = 0;
size_type m_size = 0;
size_type m_size = 0;
};
template<typename T, bool CONST>
class VectorIterator
{
public:
using value_type = T;
using data_type = maybe_const_t<CONST, T>;
public:
VectorIterator() = default;
template<bool C>
VectorIterator(const VectorIterator<T, C>& other, enable_if_t<C == CONST || !C>)
: m_data(other.m_data)
{
}
VectorIterator<T, CONST>& operator++() { m_data++; return *this; }
VectorIterator<T, CONST>& operator--() { m_data--; return *this; }
VectorIterator<T, CONST> operator++(int) { auto temp = *this; ++(*this); return temp; }
VectorIterator<T, CONST> operator--(int) { auto temp = *this; --(*this); return temp; }
template<bool ENABLE = !CONST>
enable_if_t<ENABLE, T&> operator*() { ASSERT(m_data); return *m_data; }
const T& operator*() const { ASSERT(m_data); return *m_data; }
template<bool ENABLE = !CONST>
enable_if_t<ENABLE, T*> operator->() { ASSERT(m_data); return m_data; }
const T* operator->() const { ASSERT(m_data); return m_data; }
bool operator==(const VectorIterator<T, CONST>& other) const { return m_data == other.m_data; }
bool operator!=(const VectorIterator<T, CONST>& other) const { return !(*this == other); }
operator bool() const { return m_data; }
private:
VectorIterator(data_type* data) : m_data(data) { }
private:
data_type* m_data = nullptr;
friend class Vector<T>;
friend class VectorIterator<T, !CONST>;
};
template<typename T>
Vector<T>::Vector(Vector<T>&& other)
{
@ -179,10 +141,13 @@ namespace BAN
template<typename T>
Vector<T>& Vector<T>::operator=(const Vector<T>& other)
{
clear();
MUST(ensure_capacity(other.size()));
for (size_type i = 0; i < other.size(); i++)
for (size_type i = 0; i < BAN::Math::min(size(), other.size()); i++)
m_data[i] = other.m_data[i];
for (size_type i = size(); i < other.size(); i++)
new (m_data + i) T(other[i]);
for (size_type i = other.size(); i < size(); i++)
m_data[i].~T();
m_size = other.m_size;
return *this;
}
@ -204,7 +169,7 @@ namespace BAN
template<typename T>
template<typename... Args>
ErrorOr<void> Vector<T>::emplace_back(Args&&... args)
ErrorOr<void> Vector<T>::emplace_back(Args&&... args) requires is_constructible_v<T, Args...>
{
TRY(ensure_capacity(m_size + 1));
new (m_data + m_size) T(forward<Args>(args)...);
@ -214,7 +179,7 @@ namespace BAN
template<typename T>
template<typename... Args>
ErrorOr<void> Vector<T>::emplace(size_type index, Args&&... args)
ErrorOr<void> Vector<T>::emplace(size_type index, Args&&... args) requires is_constructible_v<T, Args...>
{
ASSERT(index <= m_size);
TRY(ensure_capacity(m_size + 1));
@ -256,7 +221,7 @@ namespace BAN
template<typename T>
ErrorOr<void> Vector<T>::insert(size_type index, const T& value)
{
return insert(move(T(value)), index);
return insert(index, move(T(value)));
}
template<typename T>
@ -296,7 +261,7 @@ namespace BAN
return true;
return false;
}
template<typename T>
const T& Vector<T>::operator[](size_type index) const
{
@ -340,7 +305,14 @@ namespace BAN
}
template<typename T>
ErrorOr<void> Vector<T>::resize(size_type size)
void Vector<T>::reverse()
{
for (size_type i = 0; i < m_size / 2; i++)
BAN::swap(m_data[i], m_data[m_size - i - 1]);
}
template<typename T>
ErrorOr<void> Vector<T>::resize(size_type size) requires is_default_constructible_v<T>
{
TRY(ensure_capacity(size));
if (size < m_size)
@ -353,6 +325,20 @@ namespace BAN
return {};
}
template<typename T>
ErrorOr<void> Vector<T>::resize(size_type size, const T& value) requires is_copy_constructible_v<T>
{
TRY(ensure_capacity(size));
if (size < m_size)
for (size_type i = size; i < m_size; i++)
m_data[i].~T();
if (size > m_size)
for (size_type i = m_size; i < size; i++)
new (m_data + i) T(value);
m_size = size;
return {};
}
template<typename T>
ErrorOr<void> Vector<T>::reserve(size_type size)
{

128
BAN/include/BAN/WeakPtr.h Normal file
View File

@ -0,0 +1,128 @@
#pragma once
#include <BAN/RefPtr.h>
#if __is_kernel
#include <kernel/Lock/SpinLock.h>
#endif
namespace BAN
{
template<typename T>
class Weakable;
template<typename T>
class WeakPtr;
// FIXME: Write this without using locks...
template<typename T>
class WeakLink : public RefCounted<WeakLink<T>>
{
public:
RefPtr<T> try_lock()
{
#if __is_kernel
Kernel::SpinLockGuard _(m_weak_lock);
#endif
if (m_ptr && m_ptr->try_ref())
return RefPtr<T>::adopt(m_ptr);
return nullptr;
}
bool valid() const { return m_ptr; }
void invalidate()
{
#if __is_kernel
Kernel::SpinLockGuard _(m_weak_lock);
#endif
m_ptr = nullptr;
}
private:
WeakLink(T* ptr) : m_ptr(ptr) {}
private:
T* m_ptr;
#if __is_kernel
Kernel::SpinLock m_weak_lock;
#endif
friend class RefPtr<WeakLink<T>>;
};
template<typename T>
class Weakable
{
public:
virtual ~Weakable()
{
if (m_link)
m_link->invalidate();
}
ErrorOr<WeakPtr<T>> get_weak_ptr() const
{
if (!m_link)
m_link = TRY(RefPtr<WeakLink<T>>::create((T*)this));
return WeakPtr<T>(m_link);
}
private:
mutable RefPtr<WeakLink<T>> m_link;
};
template<typename T>
class WeakPtr
{
public:
WeakPtr() = default;
WeakPtr(WeakPtr&& other) { *this = move(other); }
WeakPtr(const WeakPtr& other) { *this = other; }
WeakPtr(const RefPtr<T>& other) { *this = other; }
WeakPtr& operator=(WeakPtr&& other)
{
clear();
m_link = move(other.m_link);
return *this;
}
WeakPtr& operator=(const WeakPtr& other)
{
clear();
m_link = other.m_link;
return *this;
}
WeakPtr& operator=(const RefPtr<T>& other)
{
clear();
if (other)
m_link = MUST(other->get_weak_ptr()).move_link();
return *this;
}
RefPtr<T> lock()
{
if (m_link)
return m_link->try_lock();
return nullptr;
}
void clear() { m_link.clear(); }
bool valid() const { return m_link && m_link->valid(); }
explicit operator bool() const { return valid(); }
private:
WeakPtr(const RefPtr<WeakLink<T>>& link)
: m_link(link)
{ }
RefPtr<WeakLink<T>>&& move_link() { return move(m_link); }
private:
RefPtr<WeakLink<T>> m_link;
friend class Weakable<T>;
};
}

57
CMakeLists.txt Normal file
View File

@ -0,0 +1,57 @@
cmake_minimum_required(VERSION 3.26)
if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "banan-os")
message(FATAL_ERROR "CMAKE_SYSTEM_NAME is not banan-os")
endif ()
project(banan-os CXX C ASM)
set(BANAN_INCLUDE ${BANAN_SYSROOT}/usr/include)
set(BANAN_LIB ${BANAN_SYSROOT}/usr/lib)
set(BANAN_BIN ${BANAN_SYSROOT}/usr/bin)
set(BANAN_ETC ${BANAN_SYSROOT}/usr/etc)
set(BANAN_SHARE ${BANAN_SYSROOT}/usr/share)
set(BANAN_BOOT ${BANAN_SYSROOT}/boot)
set(CMAKE_INSTALL_BINDIR ${BANAN_BIN})
set(CMAKE_INSTALL_SBINDIR ${BANAN_BIN})
set(CMAKE_INSTALL_LIBDIR ${BANAN_LIB})
set(CMAKE_INSTALL_INCLUDEDIR ${BANAN_INCLUDE})
set(CMAKE_INSTALL_SYSCONF ${BANAN_ETC})
set(CMAKE_INSTALL_MESSAGE NEVER)
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY True)
set(CMAKE_STATIC_LIBRARY_PREFIX "")
set(CMAKE_SHARED_LIBRARY_PREFIX "")
set(BUILD_SHARED_LIBS True)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
# include headers of ${library} to ${target}
function(banan_include_headers target library)
target_include_directories(${target} PRIVATE $<TARGET_PROPERTY:${library},SOURCE_DIR>/include)
endfunction()
# include headers and link ${library} to ${target}
function(banan_link_library target library)
target_link_libraries(${target} PRIVATE ${library})
banan_include_headers(${target} ${library})
endfunction()
# add install step for all header files of target
function(banan_install_headers target)
file(GLOB_RECURSE headers RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/include *.h)
foreach(header ${headers})
get_filename_component(subdirectory ${header} DIRECTORY)
install(FILES include/${header} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${subdirectory})
endforeach()
target_include_directories(${target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
endfunction()
add_subdirectory(kernel)
add_subdirectory(bootloader)
add_subdirectory(BAN)
add_subdirectory(userspace)

24
LICENSE Normal file
View File

@ -0,0 +1,24 @@
BSD 2-Clause License
Copyright (c) 2023, Oskari Alaranta
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

135
README.md Normal file
View File

@ -0,0 +1,135 @@
[![](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fbananymous.com%2Fbanan-os%2Ftokei.json&query=%24.lines&label=total%20lines)](https://git.bananymous.com/Bananymous/banan-os)
[![](https://img.shields.io/github/commit-activity/m/Bananymous/banan-os)](https://git.bananymous.com/Bananymous/banan-os)
[![](https://img.shields.io/github/license/bananymous/banan-os)](https://git.bananymous.com/Bananymous/banan-os/src/branch/main/LICENSE)
[![](https://img.shields.io/discord/1242165176032297040?logo=discord&label=discord)](https://discord.gg/ehjGySwYdK)
# banan-os
This is my hobby operating system written in C++. Currently supports x86\_64 and i686 architectures.
You can find a live demo [here](https://bananymous.com/banan-os)
If you want to try out DOOM, you should first enter the GUI environment using the `start-gui` command. Then you can run `doom` in the GUI terminal.
### Features
#### General
- [x] Ring3 userspace
- [x] SMP (multiprocessing)
- [x] Linear framebuffer (VESA and GOP)
- [x] Network stack
- [x] ELF executable loading
- [x] AML interpreter (partial)
- [x] Basic graphical environment
- [x] Terminal emulator
- [x] Status bar
- [ ] Program launcher
- [ ] Some nice apps
- [x] ELF dynamic linking
- [x] copy-on-write memory
- [x] file mappings
- [ ] anonymous mappings
#### Drivers
- [x] NVMe disks
- [x] ATA (IDE, SATA) disks
- [x] E1000 and E1000E NICs
- [x] RTL8111/8168/8211/8411 NICs
- [x] PS2 keyboard (all scancode sets)
- [x] PS2 mouse
- [x] USB
- [x] Keyboard
- [x] Mouse
- [x] Mass storage
- [ ] Hubs
- [ ] ...
- [ ] virtio devices (network, storage)
#### Network
- [x] ARP
- [x] ICMP
- [x] IPv4
- [x] UDP
- [x] TCP (partial and buggy)
- [x] Unix domain sockets
- [ ] SSL
#### Filesystems
- [x] Virtual filesystem
- [x] Ext2
- [x] FAT12/16/32
- [x] Dev
- [x] Ram
- [x] Proc
- [ ] Sys
- [ ] 9P
#### Bootloader support
- [x] GRUB
- [x] Custom BIOS bootloader
- [ ] Custom UEFI bootloader
![screenshot from qemu running banan-os](assets/banan-os.png)
## Code structure
Each major component and library has its own subdirectory (kernel, userspace, libc, ...). Each directory contains directory *include*, which has **all** of the header files of the component. Every header is included by its absolute path.
## Building
### Needed packages
#### apt (tested on ubuntu 22.04)
```# apt install build-essential git ninja-build texinfo bison flex libgmp-dev libmpfr-dev libmpc-dev parted qemu-system-x86 cpu-checker```
#### pacman
```# pacman -S --needed base-devel git wget cmake ninja parted qemu-system-x86```
### Compilation
To build the toolchain for this os. You can run the following command.
> ***NOTE:*** The following step has to be done only once. This might take a long time since we are compiling binutils and gcc.
```sh
./bos toolchain
```
To build the os itself you can run one of the following commands. You will need root access for disk image creation/modification.
```sh
./bos qemu
./bos qemu-nographic
./bos qemu-debug
./bos bochs
```
You can also build the kernel or disk image without running it:
```sh
./bos kernel
./bos image
```
To build for other architectures set environment variable BANAN\_ARCH=*arch* (e.g. BANAN\_ARCH=i686).
To change the bootloader you can set environment variable BANAN\_BOOTLOADER; supported values are BANAN (my custom bootloader) and GRUB.
To run with UEFI set environment variable BANAN\_UEFI\_BOOT=1. You will also have to set OVMF\_PATH to the correct OVMF (default */usr/share/ovmf/x64/OVMF.fd*).
If you have corrupted your disk image or want to create new one, you can either manually delete *build/banan-os.img* and build system will automatically create you a new one or you can run the following command.
```sh
./bos image-full
```
I have also created shell completion script for zsh. You can either copy the file in _script/shell-completion/zsh/\_bos_ to _/usr/share/zsh/site-functions/_ or add the _script/shell-completion/zsh_ to your fpath in _.zshrc_.
## Contributing
As the upstream is hosted on my server https://git.bananymous.com/Bananymous/banan-os, merging contributions is not as trivial as it would be on GitHub. You can still send PRs in GitHub in which case I should be able to download the diff and apply it manually. If you want, I can also provide you an account to my git server. In this case please contact me ([email](mailto:oskari.alaranta@bananymous.com), [discord](https://discord.gg/ehjGySwYdK)).
As this is mostly a learning experience for me, I would appreciate if you first contacted me about adding new features (email, discord, issue, ...). If you send a PR about something I was planning on doing myself and you didn't ask me, I will probably just close it. Bug fixes are always welcome!
Commit message should be formatted followingly
1. First line is of the form "_Subject: Description_", where _Subject_ tells the area touched (Kernel, Shell, BuildSystem, ...) and _Description_ is brief description of the change done. First line should fit fully in 72 characters.
2. Body of the message should further describe the change and reasoning behind the change.
All commits should pass the pre-commit hook defined in _.pre-commit-config.yaml_. For instructions on how to setup pre-commit, please see https://pre-commit.com/#install.

BIN
assets/banan-os.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 384 KiB

BIN
base-sysroot.tar.gz Normal file

Binary file not shown.

View File

@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.26)
add_subdirectory(bios)

View File

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.26)
project(bootloader ASM)
set(BOOTLOADER_SOURCES
a20_line.S
boot.S
command_line.S
disk.S
elf.S
ext2.S
framebuffer.S
memory_map.S
utils.S
)
add_executable(bootloader ${BOOTLOADER_SOURCES})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
target_link_options(bootloader PRIVATE LINKER:-T,${CMAKE_CURRENT_SOURCE_DIR}/linker.ld)
target_link_options(bootloader PRIVATE -nostdlib)

168
bootloader/bios/a20_line.S Normal file
View File

@ -0,0 +1,168 @@
.code16
.section .stage2
# checks whether A20 line is enabled or disabled
# return
# ax: 1 if enabled, 0 otherwise
check_a20:
pushf
pushw %si
pushw %di
pushw %ds
pushw %es
cli
xorw %ax, %ax
movw %ax, %es
notw %ax
movw %ax, %ds
movw $0x0500, %di
movw $0x0510, %si
movb %es:(%di), %al
pushw %ax
movb %ds:(%si), %al
pushw %ax
movb $0x00, %es:(%di)
movb $0xFF, %ds:(%si)
cmpb $0xFF, %es:(%di)
pop %ax
movb %al, %ds:(%si)
pop %ax
movb %al, %es:(%di)
movw $0, %ax
je .check_a20_done
movw $1, %ax
.check_a20_done:
popw %es
popw %ds
popw %di
popw %si
popf
ret
# Try to enable A20 using PS2 controller
enable_a20_ps2:
pushf
pushw %ax
cli
# disable first port
call .enable_a20_ps2_wait1
movb $0xAD, %al
outb %al, $0x64
# read controller output
call .enable_a20_ps2_wait1
movb $0xD0, %al
outb %al, $0x64
call .enable_a20_ps2_wait2
inb $0x60, %al
pushw %ax
# write controller output
call .enable_a20_ps2_wait1
movb $0xD1, %al
outb %al, $0x64
call .enable_a20_ps2_wait1
popw %ax
orw $2, %ax
outb %al, $0x60
# enable first port
call .enable_a20_ps2_wait1
movb $0xAE, %al
outb %al, $0x64
call .enable_a20_ps2_wait1
popw %ax
popf
ret
.enable_a20_ps2_wait1:
inb $0x64, %al
test $2, %al
jnz .enable_a20_ps2_wait1
ret
.enable_a20_ps2_wait2:
inb $0x64, %al
test $1, %al
jnz .enable_a20_ps2_wait1
ret
# Check if A20 line is disabled. If it is, try to enable it
.global enable_a20
enable_a20:
pushw %ax
pushw %si
call check_a20
testw %ax, %ax
jnz .enable_a20_done
movw $a20_line_disabled_msg, %si
call puts; call print_newline
# Try to enable A20 line using bios interrupt
movw $0x2401, %ax
int $0x15
call check_a20
testw %ax, %ax
jnz .enable_a20_done
# Try to enable A20 line using ps2 controller
call enable_a20_ps2
call check_a20
testw %ax, %ax
jnz .enable_a20_done
# Try to enable A20 line using fast A20 gate
inb $0x92, %al
testb $2, %al
jnz .enable_a20_fast_done
orb $2, %al
outb %al, $0x92
.enable_a20_fast_done:
call check_a20
testw %ax, %ax
jnz .enable_a20_done
movw $a20_could_not_enable_msg, %si
call print_and_halt
.enable_a20_done:
movw $a20_line_enabled_msg, %si
call puts; call print_newline
popw %si
popw %ax
ret
.section .data
a20_line_disabled_msg:
.asciz "A20 line disabled. Trying to enable it"
a20_line_enabled_msg:
.asciz "A20 line enabled"
a20_could_not_enable_msg:
.asciz "Could not enable A20 line"

176
bootloader/bios/boot.S Normal file
View File

@ -0,0 +1,176 @@
.include "common.S"
.code16
#########################################
#
# STAGE 1 BOOTLOADER
#
# its sole purpose is to load stage2 from
# bios boot partition
#
#########################################
.section .stage1
.global stage1_main
stage1_main:
# setup segments and stack
xorw %ax, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %ss
movl $0x7C00, %esp
# save boot disk number
call read_stage2_into_memory
jmp stage2_main
.global print_and_halt
print_and_halt:
call puts
halt:
hlt
jmp halt
#########################################
#
# STAGE 2 BOOTLOADER
#
#########################################
.section .stage2
stage2_main:
# clear screen and enter 80x25 text mode
movb $0x03, %al
movb $0x00, %ah
int $0x10
# print hello message
movw $hello_msg, %si
call puts; call print_newline
lgdt gdtr
call enter_unreal_mode
movw $unreal_enter_msg, %si
call puts; call print_newline
call enable_a20
call get_memory_map
call print_newline
call read_user_command_line
call print_newline
movw $start_kernel_load_msg, %si
call puts; call print_newline
call print_memory_map
call find_root_disk
call find_root_partition
call print_root_partition_info
call print_newline
call has_ext2_filesystem
testb %al, %al
jz print_and_halt
call ext2_find_kernel
movl $ext2_inode_read_bytes, %esi
call elf_read_kernel_to_memory
call vesa_set_video_mode
cli
# kernel entry point
movl %eax, %ecx
# setup kernel parameters
movl $0xD3C60CFF, %eax
movl $banan_boot_info, %ebx
# setup protected mode
movl %cr0, %edx
orb $1, %dl
movl %edx, %cr0
# jump to protected mode
ljmpl $GDT_CODE32, $protected_mode
.code32
protected_mode:
# setup protected mode segments
movw $GDT_DATA32, %dx
movw %dx, %ds
movw %dx, %es
movw %dx, %fs
movw %dx, %gs
movw %dx, %ss
# jump to kernel entry
jmp *%ecx
.code16
enter_unreal_mode:
cli
pushw %ds
movl %cr0, %eax
orb $1, %al
movl %eax, %cr0
ljmpl $GDT_CODE16, $.enter_unreal_mode_pmode
.enter_unreal_mode_pmode:
movw $GDT_DATA32, %bx
movw %bx, %ds
andb $0xFE, %al
movl %eax, %cr0
ljmpl $0x00, $.enter_unreal_mode_unreal
.enter_unreal_mode_unreal:
popw %ds
sti
ret
.section .data
hello_msg:
.asciz "This is banan-os bootloader"
unreal_enter_msg:
.asciz "Entered unreal mode"
start_kernel_load_msg:
.asciz "Starting to load kernel"
gdt:
.quad 0x0000000000000000
.quad 0x008F9A000000FFFF # 16-bit code
.quad 0x00CF92000000FFFF # 32-bit data
.quad 0x00CF9A000000FFFF # 32-bit code
gdtr:
.short . - gdt - 1
.long gdt
banan_boot_info:
boot_command_line:
.long command_line
boot_framebuffer:
.long framebuffer
boot_memory_map:
.long memory_map
boot_kernel_paddr:
.long 0

View File

@ -0,0 +1,87 @@
.code16
.section .stage2
# fills command line buffer
# NO REGISTERS SAVED
.global read_user_command_line
read_user_command_line:
# print initial command line
movw $command_line_enter_msg, %si
call puts
movw $command_line_buffer, %si
call puts
# prepare registers for input
movw $command_line_enter_msg, %si
movw $command_line_buffer, %di
.read_user_command_line_goto_end:
cmpb $0, (%di)
jz .read_user_command_line_loop
incw %di
jmp .read_user_command_line_goto_end
.read_user_command_line_loop:
call getc
cmpb $'\b', %al
je .read_user_command_line_backspace
cmpb $0x7F, %al
je .read_user_command_line_backspace
# Not sure if some BIOSes return '\n' as enter, but check it just in case
cmpb $'\r', %al
je .read_user_command_line_done
cmpb $'\n', %al
je .read_user_command_line_done
pushw %ax
call isprint
testb %al, %al
jz .read_user_command_line_loop
popw %ax
# put byte to buffer
movb %al, (%di)
incw %di
# print byte
call putc
jmp .read_user_command_line_loop
.read_user_command_line_backspace:
# don't do anything if at the beginning
cmpw $command_line_buffer, %di
je .read_user_command_line_loop
# decrement buffer pointer
decw %di
# erase byte in display
call print_backspace
jmp .read_user_command_line_loop
.read_user_command_line_done:
# null terminate command line
movb $0, (%di)
call print_newline
ret
.section .data
command_line_enter_msg:
.asciz "cmdline: "
.global command_line
command_line:
# 100 character command line
command_line_buffer:
.ascii "root=/dev/sda2"
.skip 100 - (. - command_line_buffer)

3
bootloader/bios/common.S Normal file
View File

@ -0,0 +1,3 @@
.set GDT_CODE16, 0x08
.set GDT_DATA32, 0x10
.set GDT_CODE32, 0x18

518
bootloader/bios/disk.S Normal file
View File

@ -0,0 +1,518 @@
# FIXME: don't assume 512 byte sectors
.set SECTOR_SIZE_SHIFT, 9
.set SECTOR_SIZE, 1 << SECTOR_SIZE_SHIFT
.code16
.section .stage1
.global stage2_start
.global stage2_end
# check that drive has int13 ext
# dl: drive number
# returns only if drive does have the extension
drive_has_int13_ext:
pusha
movb $0x41, %ah
movw $0x55AA, %bx
int $0x13
jc .drive_has_int13_ext_no_int13_ext
popa
ret
.drive_has_int13_ext_no_int13_ext:
mov $no_int13_ext_msg, %si
jmp print_and_halt
# read sectors from disk
# bx:eax: lba start
# cx: lba count (has to less than 0x80)
# dl: drive number
# ds:di: physical address
# returns only on success
.global read_from_disk
read_from_disk:
pusha
call drive_has_int13_ext
# prepare disk read packet
movw $disk_address_packet, %si
movb $0x10, 0x00(%si) # packet size
movb $0x00, 0x01(%si) # always 0
movw %cx, 0x02(%si) # lba count
movw %di, 0x04(%si) # offset
movw %ds, 0x06(%si) # segment
movl %eax, 0x08(%si) # 32 bit lower lba
movw %bx, 0x0C(%si) # 16 bit upper lba
movw $0, 0x0E(%si) # zero
# issue read command
mov $0x42, %ah
int $0x13
jc .read_from_disk_failed
popa
ret
.read_from_disk_failed:
mov $read_from_disk_msg, %si
jmp print_and_halt
# Reads GPT header into gpt_header buffer
# dl: drive number
# return:
# ax: 1 if has GPT header, 0 otherwise
.global read_gpt_header
read_gpt_header:
pushw %bx
pushw %cx
pushw %di
xorw %bx, %bx
movl $1, %eax
movw $1, %cx
movw $gpt_header, %di
call read_from_disk
xorw %bx, %bx
movw $1, %ax
# check if header starts with 'EFI PART'
cmpl $0x20494645, (gpt_header + 0)
cmovnew %bx, %ax
cmpl $0x54524150, (gpt_header + 4)
cmovnew %bx, %ax
popw %di
popw %cx
popw %bx
ret
# Find bios boot partition from boot drive
# returns:
# bx:eax: first lba
# cx: sector count
find_stage2_partition:
# read boot disk GPT header
movb (boot_disk_number), %dl
call read_gpt_header
testb %al, %al
jz .find_stage2_partition_not_gpt
# eax := entry_count
movl (gpt_header + 80), %eax
test %eax, %eax
jz .find_stage2_partition_not_found
# edx:eax := eax * entry_size
mull (gpt_header + 84)
test %edx, %edx
jnz .find_stage2_partition_too_big_entries
# FIXME: read one entry array section at a time
# sector count := (arr_size + SECTOR_SIZE - 1) / SECTOR_SIZE
movl %eax, %ecx
shrl $SECTOR_SIZE_SHIFT, %ecx
# start lba
movl (gpt_header + 72), %eax
movw (gpt_header + 76), %bx
movw $gpt_entry_data, %di
movw $bios_boot_guid, %si
movb (boot_disk_number), %dl
call read_from_disk
# NOTE: 'only' 0xFFFF partitions supported,
# although read will fail with more than 0x80
movw (gpt_header + 80), %cx
.find_stage2_partition_loop_gpt_entries:
pushw %cx
movw $16, %cx
call memcmp
popw %cx
testb %al, %al
jnz .find_stage2_partition_found
# add entry size to entry pointer
addw (gpt_header + 84), %di
loop .find_stage2_partition_loop_gpt_entries
# fall through to not found case
.find_stage2_partition_not_found:
movw $no_bios_boot_partition_msg, %si
jmp print_and_halt
.find_stage2_partition_not_gpt:
movw $not_gpt_partition_msg, %si
jmp print_and_halt
.find_stage2_partition_too_big_entries:
movw $too_gpt_big_entries_msg, %si
jmp print_and_halt
.find_stage2_partition_found:
# first lba
movl 32(%di), %eax
movw 36(%di), %bx
# count := last lba - first lba + 1
movl 40(%di), %ecx
subl %eax, %ecx
incl %ecx
ret
# reads stage2 into memory
# dl: boot drive number
# returns only on success
.global read_stage2_into_memory
read_stage2_into_memory:
movb %dl, (boot_disk_number)
# push stage2 sector count
movl $stage2_end, %eax
subl $stage2_start, %eax
addl $(SECTOR_SIZE - 1), %eax
movl $SECTOR_SIZE, %ecx
xorl %edx, %edx
divl %ecx
pushl %eax
call find_stage2_partition
movb (boot_disk_number), %dl
popl %ecx # FIXME: validate that partition has enough sectors
movw $stage2_start, %di
call read_from_disk
ret
# 21686148-6449-6E6F-744E-656564454649
.align 4
bios_boot_guid:
.long 0x21686148 # little endian
.word 0x6449 # little endian
.word 0x6E6F # little endian
.word 0x4E74 # big endian
.quad 0x494645646565 # big endian
boot_disk_number:
.skip 1
read_from_disk_msg:
.asciz "read error"
no_int13_ext_msg:
.asciz "no INT13 ext"
no_bios_boot_partition_msg:
.asciz "no bios boot"
too_gpt_big_entries_msg:
.asciz "too big GPT array"
not_gpt_partition_msg:
.asciz "not GPT"
.section .stage2
# check if drive exists
# dl: drive number
# return:
# al: 1 if disk is usable, 0 otherwise
drive_exists:
pusha
movb $0x48, %ah
movw $disk_drive_parameters, %si
movw $0x1A, (disk_drive_parameters) # set buffer size
int $0x13
jc .drive_exists_nope
popa
movb $1, %al
ret
.drive_exists_nope:
popa
movb $0, %al
ret
# find root disk and populate root_disk_drive_number field
# NO REGISTERS SAVED
.global find_root_disk
find_root_disk:
movb $0x80, %dl
.find_root_disk_loop:
call drive_exists
testb %al, %al
jz .find_root_disk_not_found
# read GPT header
xorw %bx, %bx
movl $1, %eax
movw $1, %cx
movw $gpt_header, %di
call read_from_disk
# confirm header (starts with 'EFI PART')
cmpl $0x20494645, (gpt_header + 0)
jne .find_root_disk_next_disk
cmpl $0x54524150, (gpt_header + 4)
jne .find_root_disk_next_disk
# compare disk GUID
movw $root_disk_guid, %si
movw $(gpt_header + 56), %di
movw $16, %cx
call memcmp
testb %al, %al
jz .find_root_disk_next_disk
movw $root_disk_found_msg, %si
call puts; call print_newline
movb %dl, (root_disk_drive_number)
ret
.find_root_disk_next_disk:
incb %dl
jmp .find_root_disk_loop
.find_root_disk_not_found:
movw $root_disk_not_found_msg, %si
jmp print_and_halt
# finds root partition from root disk
# fills root_partition_entry data structure
# NOTE: assumes GPT header is in `gpt_header`
# NO REGISTERS SAVED
# return:
# dl: drive number
# ecx: sector count (capped at 0xFFFFFFFF)
# bx:eax: first sector
.global find_root_partition
find_root_partition:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
# esp + 0: 8 byte entry array lba
movl (gpt_header + 72), %eax
movl %eax, 0(%esp)
movl (gpt_header + 76), %eax
movl %eax, 4(%esp)
# FIXME: check that bits 48-63 are zero
# esp + 8: 4 byte entries per sector
xorl %edx, %edx
movl $SECTOR_SIZE, %eax
divl (gpt_header + 84)
movl %eax, 8(%esp)
# esp + 12: 4 byte entries remaining
movl (gpt_header + 80), %eax
testl %eax, %eax
jz .find_root_partition_not_found
movl %eax, 12(%esp)
.find_root_partition_read_entry_section:
movl 0(%esp), %eax
movl 4(%esp), %ebx
movw $1, %cx
movb (root_disk_drive_number), %dl
movw $sector_buffer, %di
call read_from_disk
# ecx: min(entries per section, entries remaining)
movl 8(%esp), %ecx
cmpl 12(%esp), %ecx
jae .find_root_partition_got_entry_count
movl 12(%esp), %ecx
.find_root_partition_got_entry_count:
# update entries remaining
subl %ecx, 12(%esp)
# si: entry pointer
movw $sector_buffer, %si
.find_root_partition_loop_entries:
# temporarily save cx in dx
movw %cx, %dx
# check that entry is used
movw $16, %cx
movw $zero_guid, %di
call memcmp
test %al, %al
jnz .find_root_partition_next_entry
# compare entry guid to root guid
movw $16, %cx
addw $16, %si
movw $root_partition_guid, %di
call memcmp
subw $16, %si
testb %al, %al
jnz .find_root_partition_found
.find_root_partition_next_entry:
# restore cx
movw %dx, %cx
# entry pointer += entry size
addw (gpt_header + 84), %si
loop .find_root_partition_loop_entries
# entry not found in this sector
# increment 8 byte entry array lba
incl 0(%esp)
adcl $0, 4(%esp)
# loop to read next section if entries remaining
cmpl $0, 12(%esp)
jnz .find_root_partition_read_entry_section
.find_root_partition_not_found:
movw $root_partition_not_found_msg, %si
jmp print_and_halt
.find_root_partition_found:
# copy entry to buffer
movw $root_partition_entry, %di
movw $128, %cx
rep movsb
movw $root_partition_found_msg, %si
call puts; call print_newline
# ebx:eax := last lba
movl (root_partition_entry + 44), %ebx
movl (root_partition_entry + 40), %eax
# ebx:eax -= first lba - 1
subl (root_partition_entry + 36), %ebx
movl (root_partition_entry + 32), %ecx
decl %ecx
subl %ecx, %eax
sbbl $0, %ebx
# ecx: min(partition count, 0xFFFFFFFF)
movl $0xFFFFFFFF, %edx
movl %eax, %ecx
testl %ebx, %ebx
cmovnzl %edx, %ecx
# ebx:eax := first lba
# FIXME: confirm ebx bits 16:31 are zero
movl (root_partition_entry + 36), %ebx
movl (root_partition_entry + 32), %eax
movb (root_disk_drive_number), %dl
leavel
ret
# print information about root partition
.global print_root_partition_info
print_root_partition_info:
pushw %ax
pushw %bx
pushw %cx
pushw %si
movw $root_partition_info_start_msg, %si
call puts;
movw $16, %bx
movw $2, %cx
movw (root_partition_entry + 38), %ax; call print_number
movw (root_partition_entry + 36), %ax; call print_number
movw (root_partition_entry + 34), %ax; call print_number
movw (root_partition_entry + 32), %ax; call print_number
movb $'-', %al; call putc
movb $'>', %al; call putc
movw (root_partition_entry + 46), %ax; call print_number
movw (root_partition_entry + 44), %ax; call print_number
movw (root_partition_entry + 42), %ax; call print_number
movw (root_partition_entry + 40), %ax; call print_number
call print_newline
popw %si
popw %cx
popw %bx
popw %ax
ret
.section .data
# These will be patched during bootloader installation
root_disk_guid:
.ascii "root disk guid "
root_partition_guid:
.ascii "root part guid "
zero_guid:
.skip 16, 0
root_disk_found_msg:
.asciz "Root disk found!"
root_disk_not_found_msg:
.asciz "Root disk not found"
root_partition_found_msg:
.asciz "Root partition found!"
root_partition_not_found_msg:
.asciz "Root partition not found"
root_partition_info_start_msg:
.asciz "Root partition: "
.section .bss
.align SECTOR_SIZE
gpt_header:
.skip SECTOR_SIZE
gpt_entry_data:
.skip SECTOR_SIZE
sector_buffer:
.skip SECTOR_SIZE
disk_address_packet:
.skip 16
disk_drive_parameters:
.skip 0x1A
.skip 2 # padding
root_disk_drive_number:
.skip 1
.skip 3 # padding
root_partition_entry:
.skip 128

320
bootloader/bios/elf.S Normal file
View File

@ -0,0 +1,320 @@
.set SECTOR_SIZE, 512
# file header field offsets
.set e_type, 16
.set e_machine, 18
.set e_version, 20
.set e_entry, 24
.set e32_phoff, 28
.set e32_shoff, 32
.set e32_flags, 36
.set e32_ehsize, 40
.set e32_phentsize, 42
.set e32_phnum, 44
.set e32_shentsize, 46
.set e32_shnum, 48
.set e32_shstrndx, 50
.set e64_phoff, 32
.set e64_shoff, 40
.set e64_flags, 48
.set e64_ehsize, 52
.set e64_phentsize, 54
.set e64_phnum, 56
.set e64_shentsize, 58
.set e64_shnum, 60
.set e64_shstrndx, 62
# e_ident offsets
.set EI_CLASS, 4
.set EI_DATA, 5
.set EI_VERSION, 6
# e_ident constants
.set ELFMAGIC, 0x464C457F
.set ELFCLASS32, 1
.set ELFCLASS64, 2
.set ELFDATA2LSB, 1
.set EV_CURRENT, 1
# e_type constants
.set ET_EXEC, 2
# program header field offsets
.set p_type, 0
.set p32_offset, 4
.set p32_vaddr, 8
.set p32_paddr, 12
.set p32_filesz, 16
.set p32_memsz, 20
.set p32_flags, 24
.set p32_align, 28
.set p64_flags, 4
.set p64_offset, 8
.set p64_vaddr, 16
.set p64_paddr, 24
.set p64_filesz, 32
.set p64_memsz, 40
.set p64_align, 48
# p_type constants
.set PT_NULL, 0
.set PT_LOAD, 1
# mask for entry point and segment loading
.set LOAD_MASK, 0x07FFFFFF
.code16
.section .stage2
# Validate file header stored in elf_file_header
# returns only on success
elf_validate_file_header:
cmpl $ELFMAGIC, (elf_file_header)
jne .elf_validate_file_header_invalid_magic
cmpb $ELFCLASS32, (elf_file_header + EI_CLASS)
je .elf_validate_file_header_class_valid
cmpb $ELFCLASS64, (elf_file_header + EI_CLASS)
je .elf_validate_file_header_class_valid
jmp .elf_validate_file_header_invalid_class
.elf_validate_file_header_class_valid:
cmpb $ELFDATA2LSB, (elf_file_header + EI_DATA)
jne .elf_validate_file_header_only_little_endian_supported
cmpb $EV_CURRENT, (elf_file_header + EI_VERSION)
jne .elf_validate_file_header_not_current_version
cmpl $EV_CURRENT, (elf_file_header + e_version)
jne .elf_validate_file_header_not_current_version
cmpw $ET_EXEC, (elf_file_header + e_type)
jne .elf_validate_file_header_not_executable
ret
.elf_validate_file_header_invalid_magic:
movw $elf_validate_file_header_invalid_magic_msg, %si
jmp print_and_halt
.elf_validate_file_header_invalid_class:
movw $elf_validate_file_header_invalid_class_msg, %si
jmp print_and_halt
.elf_validate_file_header_only_little_endian_supported:
movw $elf_validate_file_header_only_little_endian_supported_msg, %si
jmp print_and_halt
.elf_validate_file_header_not_current_version:
movw $elf_validate_file_header_not_current_version_msg, %si
jmp print_and_halt
.elf_validate_file_header_not_executable:
movw $elf_validate_file_header_not_executable_msg, %si
jmp print_and_halt
# reads memory specified by 32 bit elf_program_header to memory
elf_read_program_header32_to_memory:
pushal
pushl %ebp
movl %esp, %ebp
# memset p_filesz -> p_memsz to 0
movl (elf_program_header + p32_filesz), %ebx
movl (elf_program_header + p32_vaddr), %edi
andl $LOAD_MASK, %edi
addl %ebx, %edi
movl (elf_program_header + p32_memsz), %ecx
subl %ebx, %ecx
xorb %al, %al; call memset32
# read file specified in program header to memory
movl (elf_program_header + p32_offset), %eax
movl (elf_program_header + p32_vaddr), %edi
andl $LOAD_MASK, %edi
movl (elf_program_header + p32_filesz), %ecx
call *%esi
leavel
popal
ret
# reads memory specified by 64 bit elf_program_header to memory
elf_read_program_header64_to_memory:
pushal
pushl %ebp
movl %esp, %ebp
# memset p_filesz -> p_memsz to 0
movl (elf_program_header + p64_filesz), %ebx
movl (elf_program_header + p64_vaddr), %edi
andl $LOAD_MASK, %edi
addl %ebx, %edi
movl (elf_program_header + p64_memsz), %ecx
subl %ebx, %ecx
xorb %al, %al; call memset32
# read file specified in program header to memory
movl (elf_program_header + p64_offset), %eax
movl (elf_program_header + p64_vaddr), %edi
andl $LOAD_MASK, %edi
movl (elf_program_header + p64_filesz), %ecx
call *%esi
leavel
popal
ret
# read callback format
# eax: first byte
# ecx: byte count
# edi: buffer
# returns only on success
# reads kernel to memory
# esi: callback for reading from kernel image
# return:
# eax: kernel entry address
.global elf_read_kernel_to_memory
elf_read_kernel_to_memory:
pushal
pushl %ebp
movl %esp, %ebp
subl $2, %esp
# read start of file header
movl $0, %eax
movl $24, %ecx
movl $elf_file_header, %edi
call *%esi
call elf_validate_file_header
# determine file header size
movl $52, %ecx
movl $64, %edx
cmpb $ELFCLASS64, (elf_file_header + EI_CLASS)
cmovel %edx, %ecx
# read full file header
movl $0, %eax
movl $elf_file_header, %edi
call *%esi
# verify that e_phoff fits in 32 bits
cmpb $ELFCLASS64, (elf_file_header + EI_CLASS)
jne .elf_read_kernel_to_memory_valid_offset
cmpl $0, (elf_file_header + e64_phoff + 4)
jnz .elf_read_kernel_to_memory_unsupported_offset
.elf_read_kernel_to_memory_valid_offset:
# read architecture phentsize and phnum to fixed locations
movw (elf_file_header + e32_phentsize), %ax
movw (elf_file_header + e32_phnum), %bx
movl (elf_file_header + e32_phoff), %ecx
cmpb $ELFCLASS64, (elf_file_header + EI_CLASS)
cmovew (elf_file_header + e64_phentsize), %ax
cmovew (elf_file_header + e64_phnum), %bx
cmovel (elf_file_header + e64_phoff), %ecx
movw %ax, (elf_file_header_phentsize)
movw %bx, (elf_file_header_phnum)
movl %ecx, (elf_file_header_phoff)
# current program header
movw $0, -2(%ebp)
.elf_read_kernel_to_memory_loop_program_headers:
movw -2(%ebp), %cx
cmpw (elf_file_header_phnum), %cx
jae .elf_read_kernel_to_memory_done
# eax := program_header_index * e_phentsize + e_phoff
xorl %eax, %eax
movw %cx, %ax
xorl %ebx, %ebx
movw (elf_file_header_phentsize), %bx
mull %ebx
addl (elf_file_header_phoff), %eax
jc .elf_read_kernel_to_memory_unsupported_offset
# determine program header size
movl $32, %ecx
movl $56, %edx
cmpb $ELFCLASS64, (elf_file_header + EI_CLASS)
cmovel %edx, %ecx
# read program header
movl $elf_program_header, %edi
call *%esi
# test if program header is NULL header
cmpl $PT_NULL, (elf_program_header + p_type)
je .elf_read_kernel_to_memory_null_program_header
# confirm that the program header is loadable
cmpl $PT_LOAD, (elf_program_header + p_type)
jne .elf_read_kernel_to_memory_not_loadable_header
# read program header to memory
movl $elf_read_program_header32_to_memory, %eax
movl $elf_read_program_header64_to_memory, %ebx
cmpb $ELFCLASS64, (elf_file_header + EI_CLASS)
cmovel %ebx, %eax
call *%eax
.elf_read_kernel_to_memory_null_program_header:
incw -2(%ebp)
jmp .elf_read_kernel_to_memory_loop_program_headers
.elf_read_kernel_to_memory_done:
leavel
popal
# set kernel entry address
movl (elf_file_header + e_entry), %eax
andl $LOAD_MASK, %eax
ret
.elf_read_kernel_to_memory_unsupported_offset:
movw $elf_read_kernel_to_memory_unsupported_offset_msg, %si
jmp print_and_halt
.elf_read_kernel_to_memory_not_loadable_header:
movw $elf_read_kernel_to_memory_not_loadable_header_msg, %si
jmp print_and_halt
.section .data
elf_validate_file_header_invalid_magic_msg:
.asciz "ELF: file has invalid ELF magic"
elf_validate_file_header_invalid_class_msg:
.asciz "ELF: file has invalid ELF class"
elf_validate_file_header_only_little_endian_supported_msg:
.asciz "ELF: file is not in little endian format"
elf_validate_file_header_not_current_version_msg:
.asciz "ELF: file is not in current ELF version"
elf_validate_file_header_not_executable_msg:
.asciz "ELF: file is not an executable"
elf_read_kernel_to_memory_unsupported_offset_msg:
.asciz "ELF: unsupported offset (only 32 bit offsets supported)"
elf_read_kernel_to_memory_not_loadable_header_msg:
.asciz "ELF: kernel contains non-loadable program header"
.section .bss
elf_file_header:
.skip 64
elf_file_header_phentsize:
.skip 2
elf_file_header_phnum:
.skip 2
elf_file_header_phoff:
.skip 4 # NOTE: only 32 bit offsets are supported
elf_program_header:
.skip 56

749
bootloader/bios/ext2.S Normal file
View File

@ -0,0 +1,749 @@
# FIXME: don't assume 512 byte sectors
.set SECTOR_SHIFT, 9
.set SECTOR_SIZE, 1 << SECTOR_SHIFT
.set EXT2_MAX_BLOCK_SIZE, 4096
.set EXT2_SUPERBLOCK_SIZE, 264
.set EXT2_BGD_SHIFT, 5
.set EXT2_BGD_SIZE, 1 << EXT2_BGD_SHIFT
.set EXT2_INODE_SIZE_MAX, 256
.set EXT2_ROOT_INO, 2
.set EXT2_GOOD_OLD_REV, 0
# inode types
.set EXT2_S_IMASK, 0xF000
.set EXT2_S_IFDIR, 0x4000
.set EXT2_S_IFREG, 0x8000
# superblock offsets
.set s_first_data_block, 20
.set s_log_block_size, 24
.set s_inodes_per_group, 40
.set s_magic, 56
.set s_rev_level, 76
.set s_inode_size, 88
# block group descriptor offsets
.set bg_inode_table, 8
# inode offsets
.set i_mode, 0
.set i_size, 4
.set i_block, 40
.code16
.section .stage2
# checks whether partition contains ext2 filesystem.
# fills ext2_superblock_buffer
# dl: drive number
# ecx: sector count
# bx:eax: first sector
# return:
# al: 1 if is ext2, 0 otherwise
# si: error message on error
.global has_ext2_filesystem
has_ext2_filesystem:
pushl %ecx
pushw %bx
pushw %di
# fill ext2_partition_first_sector
movw $0, (ext2_partition_first_sector + 6)
movw %bx, (ext2_partition_first_sector + 4)
movl %eax, (ext2_partition_first_sector + 0)
# fill ext2_drive_number
movb %dl, (ext2_drive_number)
cmpl $3, %ecx
jb .has_ext2_filesystem_does_not_fit
# one sector
movw $1, %cx
# from byte offset 1024
addl $(1024 / SECTOR_SIZE), %eax
adcw $0, %bx
# into sector buffer
movw $ext2_block_buffer, %di
call read_from_disk
# copy superblock to its buffer
movw $ext2_block_buffer, %si
movw $ext2_superblock_buffer, %di
movw $EXT2_SUPERBLOCK_SIZE, %cx
rep movsb
# verify magic
cmpw $0xEF53, (ext2_superblock_buffer + s_magic)
jne .has_ext2_filesystem_invalid_magic
# verify block size
# verify shift fits in one byte
movl (ext2_superblock_buffer + s_log_block_size), %ecx
testl $0xFFFFFF00, %ecx
jnz .has_ext2_filesystem_unsupported_block_size
# verify 1024 << s_log_block_size <= EXT2_MAX_BLOCK_SIZE
movl $1024, %eax
shll %cl, %eax
cmpl $EXT2_MAX_BLOCK_SIZE, %eax
ja .has_ext2_filesystem_unsupported_block_size
# fill block size and shift
movl %eax, (ext2_block_size)
addl $10, %ecx
movl %ecx, (ext2_block_shift)
# fill inode size
movl $128, %eax
cmpl $EXT2_GOOD_OLD_REV, (ext2_superblock_buffer + s_rev_level)
cmovnel (ext2_superblock_buffer + s_inode_size), %eax
movl %eax, (ext2_inode_size)
movb $1, %al
jmp .has_ext2_filesystem_done
.has_ext2_filesystem_does_not_fit:
movw $root_partition_does_not_fit_ext2_filesystem_msg, %si
movb $0, %al
jmp .has_ext2_filesystem_done
.has_ext2_filesystem_invalid_magic:
movw $root_partition_has_invalid_ext2_magic_msg, %si
movb $0, %al
jmp .has_ext2_filesystem_done
.has_ext2_filesystem_unsupported_block_size:
movw $root_partition_has_unsupported_ext2_block_size_msg, %si
movb $0, %al
jmp .has_ext2_filesystem_done
.has_ext2_filesystem_done:
popw %di
popw %bx
popl %ecx
ret
# reads block in to ext2_block_buffer
# eax: block number
ext2_read_block:
pushal
# ecx := sectors_per_block := block_size / sector_size
movl (ext2_block_size), %ecx
shrl $SECTOR_SHIFT, %ecx
# ebx:eax := block * sectors_per_block + (ext2_partition_first_sector)
xorl %ebx, %ebx
mull %ecx
addl (ext2_partition_first_sector + 0), %eax
adcl (ext2_partition_first_sector + 4), %ebx
movw $ext2_block_buffer, %di
movb (ext2_drive_number), %dl
call read_from_disk
popal
ret
# reads block group descrtiptor into ext2_block_group_descriptor
# eax: block group
ext2_read_block_group_descriptor:
pushal
# ebx := bgd_block_byte_offset := (s_first_data_block + 1) * block_size
# := (s_first_data_block + 1) << ext2_block_shift
movl (ext2_superblock_buffer + s_first_data_block), %ebx
incl %ebx
movb (ext2_block_shift), %cl
shll %cl, %ebx
# eax := bgd_byte_offset := bgd_block_byte_offset + EXT2_BGD_SIZE * block_group;
# := bgd_block_byte_offset + (block_group << EXT2_BGD_SHIFT)
movb $EXT2_BGD_SHIFT, %cl
shll %cl, %eax
addl %ebx, %eax
# eax: bgd_block := bgd_byte_offset / block_size
# ebx: bgd_offset := bgd_byte_offset % block_size
xorl %edx, %edx
divl (ext2_block_size)
movl %edx, %ebx
call ext2_read_block
# esi := &ext2_block_buffer + bgd_offset := ebx + &ext2_block_buffer
# edi := &ext2_block_group_descriptor_buffer
movl %ebx, %esi
addl $ext2_block_buffer, %esi
movl $ext2_block_group_descriptor_buffer, %edi
movw $EXT2_BGD_SIZE, %cx
rep movsb
popal
ret
# reads inode into ext2_inode_buffer
# eax: ino
ext2_read_inode:
pushal
# eax := block_group = (ino - 1) / s_inodes_per_group
# ebx := inode_index = (ino - 1) % s_inodes_per_group
xorl %edx, %edx
decl %eax
divl (ext2_superblock_buffer + s_inodes_per_group)
movl %edx, %ebx
call ext2_read_block_group_descriptor
# eax := inode_table_block := (inode_index * inode_size) / block_size
# ebx := inode_table_offset := (inode_index * inode_size) % block_size
movl %ebx, %eax
mull (ext2_inode_size)
divl (ext2_block_size)
movl %edx, %ebx
# eax := filesystem_block := eax + bg_inode_table
addl (ext2_block_group_descriptor_buffer + bg_inode_table), %eax
movb (ext2_drive_number), %dl
call ext2_read_block
# copy inode memory
# esi := inode_table_offset + ext2_block_buffer := edx + ext2_block_buffer
movl %ebx, %esi
addl $ext2_block_buffer, %esi
# edi := ext2_inode_buffer
movl $ext2_inode_buffer, %edi
# ecx := inode_size
movl (ext2_inode_size), %ecx
rep movsb
# reset indirect cache to zero
movl $0, (ext2_inode_indirect_number)
.ext2_read_inode_done:
popal
ret
# gets block index from n'th data block in inode stored in ext2_inode_buffer
# eax: data block index
# return:
# eax: block index
ext2_data_block_index:
pushl %ebx
pushl %ecx
pushl %edx
pushl %esi
pushl %edi
# ebx := max_data_blocks := (file_size + block_size - 1) / block_size
# := (i_size + ext2_block_size - 1) >> ext2_block_shift
# cl := ext2_block_shift
movl (ext2_inode_buffer + i_size), %ebx
addl (ext2_block_size), %ebx
decl %ebx
movb (ext2_block_shift), %cl
shrl %cl, %ebx
# verify data block is within bounds
cmpl %ebx, %eax
jae .ext2_data_block_index_out_of_bounds
# check if this is direct block access
cmpl $12, %eax
jb .ext2_data_block_index_direct
subl $12, %eax
# cl := indices_per_block_shift := ext2_block_shift - 2
# ebx := comp
subb $2, %cl
movl $1, %ebx
shll %cl, %ebx
# check if this is singly indirect block access
cmpl %ebx, %eax
jb .ext2_data_block_index_singly_indirect
subl %ebx, %eax
shll %cl, %ebx
# check if this is doubly indirect block access
cmpl %ebx, %eax
jb .ext2_data_block_index_doubly_indirect
subl %ebx, %eax
shll %cl, %ebx
# check if this is triply indirect block access
cmpl %ebx, %eax
jb .ext2_data_block_index_triply_indirect
# otherwise this is invalid access
jmp .ext2_data_block_index_invalid
.ext2_data_block_index_direct:
movl $(ext2_inode_buffer + i_block), %esi
movl (%esi, %eax, 4), %eax
jmp .ext2_data_block_index_done
.ext2_data_block_index_singly_indirect:
movl %eax, %ebx
movl (ext2_inode_buffer + i_block + 12 * 4), %eax
movw $1, %cx
jmp .ext2_data_block_index_indirect
.ext2_data_block_index_doubly_indirect:
movl %eax, %ebx
movl (ext2_inode_buffer + i_block + 13 * 4), %eax
movw $2, %cx
jmp .ext2_data_block_index_indirect
.ext2_data_block_index_triply_indirect:
movl %eax, %ebx
movl (ext2_inode_buffer + i_block + 14 * 4), %eax
movw $3, %cx
jmp .ext2_data_block_index_indirect
# eax := current block
# ebx := index
# cx := depth
.ext2_data_block_index_indirect:
# edx := cache index := (index & ~(block_size / 4 - 1)) | depth
# := (index & -(block_size >> 2)) | depth
movl (ext2_block_size), %edx
shrl $2, %edx
negl %edx
andl %ebx, %edx
orw %cx, %dx
# check whether this block is already cached
cmpl $0, (ext2_inode_indirect_number)
je .ext2_data_block_index_indirect_no_cache
cmpl %edx, (ext2_inode_indirect_number)
je .ext2_data_block_index_indirect_cached
.ext2_data_block_index_indirect_no_cache:
# update cache block number, will be cached when found
movl %edx, (ext2_inode_indirect_number)
# eax := current block
# ebx := index
# cx := depth
.ext2_data_block_index_indirect_loop:
call ext2_read_block
# store depth and index
pushw %cx
pushl %ebx
cmpw $1, %cx
jbe .ext2_data_block_index_no_shift
# cl := shift
movb (ext2_block_shift), %al
subb $2, %al
decb %cl
mulb %cl
movb %al, %cl
# ebx := ebx >> shift
shrl %cl, %ebx
.ext2_data_block_index_no_shift:
# edx := index of next block (ebx & (block_size / 4 - 1))
movl (ext2_block_size), %edx
shrl $2, %edx
decl %edx
andl %ebx, %edx
# eax := next block
movl $ext2_block_buffer, %esi
movl (%esi, %edx, 4), %eax
# restore depth and index
popl %ebx
popw %cx
loop .ext2_data_block_index_indirect_loop
# cache last read block
movw $ext2_block_buffer, %si
movw $ext2_inode_indirect_buffer, %di
movw (ext2_block_size), %cx
rep movsb
jmp .ext2_data_block_index_done
.ext2_data_block_index_out_of_bounds:
movw $ext2_data_block_index_out_of_bounds_msg, %si
call puts; call print_newline
movl $0, %eax
jmp .ext2_data_block_index_done
.ext2_data_block_index_invalid:
movw $ext2_data_block_index_invalid_msg, %si
call puts; call print_newline
movl $0, %eax
jmp .ext2_data_block_index_done
.ext2_data_block_index_indirect_cached:
movl $ext2_inode_indirect_buffer, %esi
movl (ext2_block_size), %edx
shrl $2, %edx
decl %edx
andl %edx, %ebx
movl (%esi, %ebx, 4), %eax
.ext2_data_block_index_done:
popl %edi
popl %esi
popl %edx
popl %ecx
popl %ebx
ret
# read bytes from inode (implements read callback)
# eax: first byte
# ecx: byte count
# edi: buffer
# returns only on success
.global ext2_inode_read_bytes
ext2_inode_read_bytes:
pushal
pushl %ebp
movl %esp, %ebp
subl $8, %esp
# save read info
movl %eax, 0(%esp)
movl %ecx, 4(%esp)
# eax := first_byte / block_size
# edx := first_byte % block_size
# when edx == 0, no partial read needed
xorl %edx, %edx
divl (ext2_block_size)
testl %edx, %edx
jz .ext2_inode_read_bytes_no_partial_start
# get data block index and read block
call ext2_data_block_index
call ext2_read_block
# ecx := byte count (min(block_size - edx, remaining_bytes))
movl (ext2_block_size), %ecx
subl %edx, %ecx
cmpl %ecx, 4(%esp)
cmovbl 4(%esp), %ecx
# update remaining read info
addl %ecx, 0(%esp)
subl %ecx, 4(%esp)
# esi := start sector data (block_buffer + index * SECTOR_SIZE)
movl $ext2_block_buffer, %esi
addl %edx, %esi
call memcpy32
# check if all sectors are read
cmpl $0, 4(%esp)
je .ext2_inode_read_bytes_done
.ext2_inode_read_bytes_no_partial_start:
# eax := data block index (byte_start / block_size)
movl 0(%esp), %eax
movb (ext2_block_shift), %cl
shrl %cl, %eax
# get data block index and read block
call ext2_data_block_index
call ext2_read_block
# calculate bytes to copy (min(block_size, remaining_bytes))
movl (ext2_block_size), %ecx
cmpl %ecx, 4(%esp)
cmovbl 4(%esp), %ecx
# update remaining read info
addl %ecx, 0(%esp)
subl %ecx, 4(%esp)
movl $ext2_block_buffer, %esi
call memcpy32
# read next block if more sectors remaining
cmpl $0, 4(%esp)
jnz .ext2_inode_read_bytes_no_partial_start
.ext2_inode_read_bytes_done:
leavel
popal
ret
# find inode in inside directory inode stored in ext2_inode_buffer
# store the found inode in ext2_inode_buffer
# si: name string
# cx: name length
# return:
# eax: ino if inode was found, 0 otherwise
ext2_directory_find_inode:
pushl %ebx
pushw %cx
pushw %dx
pushw %si
pushw %di
pushl %ebp
movl %esp, %ebp
subl $8, %esp
# 0(%esp) := name length
movw %cx, 0(%esp)
# 2(%esp) := name string
movw %si, 2(%esp)
# verify that the name is <= 0xFF bytes
cmpw $0xFF, %cx
ja .ext2_directory_find_inode_not_found
# ebx := max data blocks: ceil(i_size / block_size)
movl (ext2_inode_buffer + i_size), %ebx
addl (ext2_block_size), %ebx
decl %ebx
movb (ext2_block_shift), %cl
shrl %cl, %ebx
jz .ext2_directory_find_inode_not_found
# 4(%esp) := current block
movl $0, 4(%esp)
.ext2_directory_find_inode_block_read_loop:
# get next block index
movl 4(%esp), %eax
call ext2_data_block_index
test %eax, %eax
jz .ext2_directory_find_inode_next_block
# read current block
call ext2_read_block
# dx := current entry pointer
movw $ext2_block_buffer, %si
.ext2_directory_find_inode_loop_entries:
# temporarily store entry pointer in dx
movw %si, %dx
# check if name length matches
# cx := name length
movw 0(%esp), %cx
cmpb 6(%si), %cl
jne .ext2_directory_find_inode_next_entry
# si := entry name
addw $8, %si
# di := asked name
movw 2(%esp), %di
# check if name matches
call memcmp
test %al, %al
# NOTE: dx contains entry pointer
jnz .ext2_directory_find_inode_found
.ext2_directory_find_inode_next_entry:
# restore si
movw %dx, %si
# go to next entry if this block contains one
addw 4(%si), %si
movw $ext2_block_buffer, %di
addw (ext2_block_size), %di
cmpw %di, %si
jb .ext2_directory_find_inode_loop_entries
.ext2_directory_find_inode_next_block:
incl 4(%esp)
cmpl %ebx, 4(%esp)
jb .ext2_directory_find_inode_block_read_loop
.ext2_directory_find_inode_not_found:
xorb %al, %al
jmp .ext2_directory_find_inode_done
.ext2_directory_find_inode_found:
# extract ino and read it to ext2_inode_buffer
movw %dx, %si
movl 0(%si), %eax
call ext2_read_inode
.ext2_directory_find_inode_done:
leavel
popw %di
popw %si
popw %dx
popw %cx
popl %ebx
ret
# search for kernel file from filesystem
# returns only on success
.global ext2_find_kernel
ext2_find_kernel:
pushl %eax
pushw %cx
pushw %di
pushw %si
movl $EXT2_ROOT_INO, %eax
call ext2_read_inode
movw $kernel_path, %di
.ext2_find_kernel_loop:
movw (%di), %si
# check if this list is done
testw %si, %si
jz .ext2_find_kernel_loop_done
# check that current part is directory
movw (ext2_inode_buffer + i_mode), %ax
andw $EXT2_S_IMASK, %ax
cmpw $EXT2_S_IFDIR, %ax
jne .ext2_find_kernel_part_not_dir
# prepare registers for directory finding
movw 0(%si), %cx
addw $2, %si
# print search path
pushw %si
movw $ext2_looking_for_msg, %si
call puts
popw %si
call puts; call print_newline
# search current directory for this file
call ext2_directory_find_inode
testl %eax, %eax
jz .ext2_find_kernel_part_not_found
# loop to next part
addw $2, %di
jmp .ext2_find_kernel_loop
.ext2_find_kernel_loop_done:
# check that kernel is a regular file
movw (ext2_inode_buffer + i_mode), %ax
andw $EXT2_S_IMASK, %ax
cmpw $EXT2_S_IFREG, %ax
jne .ext2_find_kernel_not_reg
movw $ext2_kernel_found_msg, %si
call puts; call print_newline
popw %si
popw %di
popw %cx
popl %eax
ret
.ext2_find_kernel_part_not_dir:
movw $ext2_part_not_dir_msg, %si
jmp print_and_halt
.ext2_find_kernel_part_not_found:
movw $ext2_part_not_found_msg, %si
jmp print_and_halt
.ext2_find_kernel_not_reg:
movw $ext2_kernel_not_reg_msg, %si
jmp print_and_halt
.section .data
kernel_path:
.short kernel_path1
.short kernel_path2
.short 0
kernel_path1:
.short 4
.asciz "boot"
kernel_path2:
.short 15
.asciz "banan-os.kernel"
root_partition_does_not_fit_ext2_filesystem_msg:
.asciz "Root partition is too small to contain ext2 filesystem"
root_partition_has_invalid_ext2_magic_msg:
.asciz "Root partition doesn't contain ext2 magic number"
root_partition_has_unsupported_ext2_block_size_msg:
.asciz "Root partition has unsupported ext2 block size (1 KiB, 2 KiB and 4 KiB are supported)"
ext2_part_not_dir_msg:
.asciz "inode in root path is not directory"
ext2_part_not_found_msg:
.asciz " not found"
ext2_kernel_not_reg_msg:
.asciz "kernel is not a regular file"
ext2_kernel_found_msg:
.asciz "kernel found!"
ext2_data_block_index_out_of_bounds_msg:
.asciz "data block index out of bounds"
ext2_data_block_index_invalid_msg:
.asciz "data block index is invalid"
ext2_looking_for_msg:
.asciz "looking for "
.section .bss
.align SECTOR_SIZE
ext2_block_buffer:
.skip EXT2_MAX_BLOCK_SIZE
ext2_inode_indirect_buffer:
.skip EXT2_MAX_BLOCK_SIZE
ext2_inode_indirect_number:
.skip 4
ext2_partition_first_sector:
.skip 8
ext2_drive_number:
.skip 1
.skip 3 # padding
# NOTE: fits in 2 bytes
ext2_inode_size:
.skip 4
ext2_block_size:
.skip 4
ext2_block_shift:
.skip 4
ext2_superblock_buffer:
.skip EXT2_SUPERBLOCK_SIZE
ext2_block_group_descriptor_buffer:
.skip EXT2_BGD_SIZE
ext2_inode_buffer:
.skip EXT2_INODE_SIZE_MAX

View File

@ -0,0 +1,218 @@
.code16
.section .stage2
# kernel framebuffer information format
# .align 8
# .long 0xBABAB007
# .long -(0xBABAB007 + width + height + bpp)
# .long width (2 bytes used, 4 bytes for ease of calculation)
# .long height (2 bytes used, 4 bytes for ease of calculation)
# .long bpp (1 bytes used, 4 bytes for ease of calculation)
# scan memory 0x100000 -> 0x200000 for framebuffer information
# return:
# ax: target width
# bx: target height
# cx: target bpp
vesa_scan_kernel_image:
pushl %edx
pushl %esi
movl $0x100000, %esi
.vesa_scan_kernel_image_loop:
# check magic
cmpl $0xBABAB007, (%esi)
jne .vesa_scan_kernel_image_next_addr
# check checksum
movl 0x00(%esi), %edx
addl 0x04(%esi), %edx
addl 0x08(%esi), %edx
addl 0x0C(%esi), %edx
addl 0x10(%esi), %edx
testl %edx, %edx
jnz .vesa_scan_kernel_image_next_addr
# set return registers
movw 0x08(%esi), %ax
movw 0x0C(%esi), %bx
movw 0x10(%esi), %cx
jmp .vesa_scan_kernel_image_done
.vesa_scan_kernel_image_next_addr:
addl $8, %esi
cmpl $0x200000, %esi
jb .vesa_scan_kernel_image_loop
# zero out return registers
xorw %ax, %ax
xorw %bx, %bx
xorw %cx, %cx
.vesa_scan_kernel_image_done:
popl %esi
popl %edx
ret
# Find suitable video mode and save it in (vesa_target_mode)
vesa_find_video_mode:
pushal
pushl %ebp
movl %esp, %ebp
subl $6, %esp
# clear target mode and frame buffer
movw $0, (vesa_target_mode)
movl $0, (framebuffer + 0)
movl $0, (framebuffer + 4)
movl $0, (framebuffer + 8)
movl $0, (framebuffer + 12)
movw $0, (framebuffer + 16)
call vesa_scan_kernel_image
testw %ax, %ax
jz .vesa_find_video_mode_loop_modes_done
# save arguments in stack
movw %ax, -2(%ebp)
movw %bx, -4(%ebp)
movw %cx, -6(%ebp)
# get vesa information
movw $0x4F00, %ax
movw $vesa_info_buffer, %di
pushl %ebp; int $0x10; popl %ebp # BOCHS doesn't seem to reserve ebp
cmpb $0x4F, %al; jne .vesa_unsupported
cmpb $0x00, %ah; jne .vesa_error
# confirm that response starts with 'VESA'
cmpl $0x41534556, (vesa_info_buffer)
jne .vesa_error
# confirm that version is atleast 2.0
cmpw $0x0200, (vesa_info_buffer + 0x04)
jb .vesa_unsupported_version
movl (vesa_info_buffer + 0x0E), %esi
.vesa_find_video_mode_loop_modes:
cmpw $0xFFFF, (%esi)
je .vesa_find_video_mode_loop_modes_done
# get info of next mode
movw $0x4F01, %ax
movw (%esi), %cx
movw $vesa_mode_info_buffer, %di
pushl %ebp; int $0x10; popl %ebp # BOCHS doesn't seem to reserve ebp
cmpb $0x4F, %al; jne .vesa_unsupported
cmpb $0x00, %ah; jne .vesa_error
# check whether in graphics mode
testb $0x10, (vesa_mode_info_buffer + 0)
jz .vesa_find_video_mode_next_mode
# compare mode's dimensions
movw -2(%ebp), %ax; cmpw %ax, (vesa_mode_info_buffer + 0x12)
jne .vesa_find_video_mode_next_mode
movw -4(%ebp), %ax; cmpw %ax, (vesa_mode_info_buffer + 0x14)
jne .vesa_find_video_mode_next_mode
movb -6(%ebp), %al; cmpb %al, (vesa_mode_info_buffer + 0x19)
jne .vesa_find_video_mode_next_mode
# set address, pitch, type
movl (vesa_mode_info_buffer + 0x28), %esi
movl %esi, (framebuffer + 0)
movw (vesa_mode_info_buffer + 0x10), %ax
movw %ax, (framebuffer + 4)
movb $1, (framebuffer + 17)
# set width, height, bpp
movw -2(%ebp), %ax; movw %ax, (framebuffer + 8)
movw -4(%ebp), %ax; movw %ax, (framebuffer + 12)
movw -6(%ebp), %ax; movb %al, (framebuffer + 16)
movw %cx, (vesa_target_mode)
jmp .vesa_find_video_mode_loop_modes_done
.vesa_find_video_mode_next_mode:
addl $2, %esi
jmp .vesa_find_video_mode_loop_modes
.vesa_find_video_mode_loop_modes_done:
leavel
popal
ret
.vesa_unsupported:
movw $vesa_unsupported_msg, %si
jmp print_and_halt
.vesa_unsupported_version:
movw $vesa_unsupported_version_msg, %si
jmp print_and_halt
.vesa_error:
movw $vesa_error_msg, %si
jmp print_and_halt
# scan for video mode in kernel memory and set the correct one.
# when video mode is not found or does not exists,
# set it to 80x25 text mode to clear the screen.
.global vesa_set_video_mode
vesa_set_video_mode:
pushw %ax
pushw %bx
call vesa_find_video_mode
movw (vesa_target_mode), %bx
testw %bx, %bx
jz .vesa_set_target_mode_generic
movw $0x4F02, %ax
orw $0x4000, %bx
pushl %ebp; int $0x10; popl %ebp # BOCHS doesn't seem to reserve ebp
jmp .set_video_done
.vesa_set_target_mode_generic:
movb $0x03, %al
movb $0x00, %ah
pushl %ebp; int $0x10; popl %ebp # BOCHS doesn't seem to reserve ebp
.set_video_done:
popw %bx
popw %ax
ret
.section .data
vesa_error_msg:
.asciz "VESA error"
vesa_unsupported_msg:
.asciz "VESA unsupported"
vesa_unsupported_version_msg:
.asciz "VESA unsupported version"
vesa_success_msg:
.asciz "VESA success"
.section .bss
vesa_info_buffer:
.skip 512
vesa_mode_info_buffer:
.skip 256
vesa_target_mode:
.skip 2
.global framebuffer
.align 8
framebuffer:
.skip 4 # address
.skip 4 # pitch
.skip 4 # width
.skip 4 # height
.skip 1 # bpp
.skip 1 # type

17
bootloader/bios/linker.ld Normal file
View File

@ -0,0 +1,17 @@
ENTRY(stage1_main)
SECTIONS
{
. = 0x7C00;
.stage1 : { *(.stage1) }
. = ALIGN(512);
stage2_start = .;
.stage2 : { *(.stage2) }
. = ALIGN(512);
.data : { *(.data) }
stage2_end = .;
. = ALIGN(512);
.bss : { *(.bss) }
}

View File

@ -0,0 +1,132 @@
.code16
.section .stage2
# fills memory map data structure
# doesn't return on error
# NO REGISTERS SAVED
.global get_memory_map
get_memory_map:
movl $0, (memory_map_entry_count)
movl $0x0000E820, %eax
movl $0x534D4150, %edx
xorl %ebx, %ebx
movl $20, %ecx
movw $memory_map_entries, %di
clc
int $0x15
# If first call returs with CF set, the call failed
jc .get_memory_map_error
.get_memory_map_rest:
cmpl $0x534D4150, %eax
jne .get_memory_map_error
# FIXME: don't assume BIOS to always return 20 bytes
cmpl $20, %ecx
jne .get_memory_map_error
# increment entry count
incl (memory_map_entry_count)
# increment entry pointer
addw %cx, %di
# BIOS can indicate end of list by 0 in ebx
testl %ebx, %ebx
jz .get_memory_map_done
movl $0x0000E820, %eax
movl $0x534D4150, %edx
clc
int $0x15
# BIOS can indicate end of list by setting CF
jnc .get_memory_map_rest
.get_memory_map_done:
ret
.get_memory_map_error:
movw $memory_map_error_msg, %si
jmp print_and_halt
# print memory map from memory_map_entries
# NO REGISTERS SAVED
.global print_memory_map
print_memory_map:
movw $memory_map_msg, %si
call puts
call print_newline
movl (memory_map_entry_count), %edx
movw $memory_map_entries, %si
movw $16, %bx
movw $4, %cx
.loop_memory_map:
movb $' ', %al
call putc; call putc; call putc; call putc
movw 0x06(%si), %ax
call print_number
movw 0x04(%si), %ax
call print_number
movw 0x02(%si), %ax
call print_number
movw 0x00(%si), %ax
call print_number
movb $',', %al
call putc
movb $' ', %al
call putc
movw 0x0E(%si), %ax
call print_number
movw 0x0C(%si), %ax
call print_number
movw 0x0A(%si), %ax
call print_number
movw 0x08(%si), %ax
call print_number
movb $',', %al
call putc
movb $' ', %al
call putc
movw 0x12(%si), %ax
call print_number
movw 0x10(%si), %ax
call print_number
call print_newline
addw $20, %si
decl %edx
jnz .loop_memory_map
ret
.section .data
memory_map_msg:
.asciz "memmap:"
memory_map_error_msg:
.asciz "Failed to get memory map"
.section .bss
.global memory_map
memory_map:
memory_map_entry_count:
.skip 4
# 100 entries should be enough...
memory_map_entries:
.skip 20 * 100

403
bootloader/bios/utils.S Normal file
View File

@ -0,0 +1,403 @@
.include "common.S"
.set SCREEN_WIDTH, 80
.set SCREEN_HEIGHT, 25
.code16
.section .stage1
# prints character to screen
# al: ascii character to print
.global putc
putc:
pushw %ax
pushw %bx
movb $0x0E, %ah
xorb %bh, %bh
int $0x10
popw %bx
popw %ax
ret
# prints null terminated string to screen
# ds:si: string address
.global puts
puts:
pushw %si
pushw %bx
pushw %ax
movb $0x0E, %ah
xorb %bh, %bh
.puts_loop:
lodsb
test %al, %al
jz .puts_done
int $0x10
jmp .puts_loop
.puts_done:
popw %ax
popw %bx
popw %si
ret
# compares memory between addresses
# si: ptr1
# di: ptr2
# cx: bytes count
# return:
# al: 1 if equal, 0 otherwise
.global memcmp
memcmp:
# NOTE: using pusha + popa to save space
pusha
cld
repe cmpsb
popa
setzb %al
ret
.section .stage2
# read a character from keyboard
# return:
# al: ascii
# ah: bios scan code
.global getc
getc:
movb $0x00, %ah
int $0x16
ret
# prints newline to screen
.global print_newline
print_newline:
pushw %ax
movb $'\r', %al
call putc
movb $'\n', %al
call putc
pop %ax
ret
# prints backspace to screen, can go back a line
.global print_backspace
print_backspace:
pushw %ax
pushw %bx
pushw %cx
pushw %dx
# get cursor position
movb $0x03, %ah
movb $0x00, %bh
int $0x10
# don't do anyting if on first row
testb %dh, %dh
jz .print_backspace_done
# go one line up if on first column
test %dl, %dl
jz .print_backspace_go_line_up
# otherwise decrease column
decb %dl
jmp .print_backspace_do_print
.print_backspace_go_line_up:
# decrease row and set column to the last one
decb %dh
movb $(SCREEN_WIDTH - 1), %dl
.print_backspace_do_print:
# set cursor position
movb $0x02, %ah
int $0x10
# print 'empty' character (space)
mov $' ', %al
call putc
# set cursor position
movb $0x02, %ah
int $0x10
.print_backspace_done:
popw %dx
popw %cx
popw %bx
popw %ax
ret
# print number to screen
# ax: number to print
# bx: number base
# cx: min width (zero pads if shorter)
.global print_number
print_number:
pusha
pushl %ebp
movl %esp, %ebp
# save min width
subl $4, %esp
movw %cx, (%esp)
movw $print_number_buffer, %si
xorw %cx, %cx
.print_number_fill_loop:
# fill buffer with all remainders ax % bx
xorw %dx, %dx
divw %bx
movb %dl, (%si)
incw %si
incw %cx
testw %ax, %ax
jnz .print_number_fill_loop
# check if zero pad is required
cmpw (%esp), %cx
jae .print_number_print_loop
# dx: saved number count
# cx: zero pad count
movw %cx, %dx
movw (%esp), %cx
subw %dx, %cx
movb $'0', %al
.print_number_pad_zeroes:
call putc
loop .print_number_pad_zeroes
# restore number count
movw %dx, %cx
.print_number_print_loop:
decw %si
movb (%si), %al
cmpb $10, %al
jae .print_number_hex
addb $'0', %al
jmp .print_number_do_print
.print_number_hex:
addb $('a' - 10), %al
.print_number_do_print:
call putc
loop .print_number_print_loop
leavel
popa
ret
# prints 8 bit hexadecimal number to screen
# al: number to print
.global print_hex8
print_hex8:
pushw %ax
pushw %bx
pushw %cx
movw $16, %bx
movw $2, %cx
andw $0xFF, %ax
call print_number
popw %cx
popw %bx
popw %ax
ret
# prints 16 bit hexadecimal number to screen
# ax: number to print
.global print_hex16
print_hex16:
pushw %bx
pushw %cx
movw $16, %bx
movw $4, %cx
call print_number
popw %cx
popw %bx
ret
# prints 32 bit hexadecimal number to screen
# eax: number to print
.global print_hex32
print_hex32:
pushl %eax
pushw %dx
movw %ax, %dx
shrl $16, %eax;
call print_hex16
movw %dx, %ax
call print_hex16
popw %dx
popl %eax
ret
# prints 64 bit hexadecimal number to screen
# edx:eax: number to print
.global print_hex64
print_hex64:
xchgl %eax, %edx
call print_hex32
xchgl %eax, %edx
call print_hex32
ret
# test if character is printable ascii
# al: character to test
# return:
# al: 1 if is printable, 0 otherwise
.global isprint
isprint:
subb $0x20, %al
cmpb $(0x7E - 0x20), %al
ja .isprint_not_printable
movb $1, %al
ret
.isprint_not_printable:
movb $0, %al
ret
# memset with 32 bit registers
# edi: destination address
# ecx: bytes count
# al: value to set
# return:
# edi: destination address + bytes count
# ecx: 0
# other: preserved
.global memset32
memset32:
testl %ecx, %ecx
jz .memset32_done
pushf; cli
pushw %es
pushl %eax
pushl %ebx
pushl %edx
movl %cr0, %ebx
orb $1, %bl
movl %ebx, %cr0
ljmpl $GDT_CODE32, $.memset32_pmode32
.code32
.memset32_pmode32:
movw $GDT_DATA32, %dx
movw %dx, %es
movl %ecx, %edx
andl $3, %ecx
rep stosb %es:(%edi)
movl %edx, %ecx
shrl $2, %ecx
movb %al, %ah
movw %ax, %dx
shll $16, %eax
movw %dx, %ax
rep stosl %es:(%edi)
ljmpl $GDT_CODE16, $.memset32_pmode16
.code16
.memset32_pmode16:
andb $0xFE, %bl
movl %ebx, %cr0
ljmpl $0x00, $.memset32_rmode16
.memset32_rmode16:
popl %edx
popl %ebx
popl %eax
popw %es
popf
.memset32_done:
ret
# memcpy with 32 bit registers
# esi: source address
# edi: destination address
# ecx: bytes count
# return:
# esi: source address + bytes count
# edi: destination address + bytes count
# ecx: 0
# other: preserved
.global memcpy32
memcpy32:
testl %ecx, %ecx
jz .memcpy32_done
pushf; cli
pushw %ds
pushw %es
pushl %ebx
pushl %edx
movl %cr0, %ebx
orb $1, %bl
movl %ebx, %cr0
ljmpl $GDT_CODE32, $.memcpy32_pmode32
.code32
.memcpy32_pmode32:
movw $GDT_DATA32, %dx
movw %dx, %ds
movw %dx, %es
movl %ecx, %edx
andl $3, %ecx
rep movsb %ds:(%esi), %es:(%edi)
movl %edx, %ecx
shrl $2, %ecx
rep movsl %ds:(%esi), %es:(%edi)
ljmpl $GDT_CODE16, $.memcpy32_pmode16
.code16
.memcpy32_pmode16:
andb $0xFE, %bl
movl %ebx, %cr0
ljmpl $0x00, $.memcpy32_rmode16
.memcpy32_rmode16:
popl %edx
popl %ebx
popw %es
popw %ds
popf
.memcpy32_done:
ret
.section .bss
# enough for base 2 printing
print_number_buffer:
.skip 16

1
bootloader/installer/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build/

View File

@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.26)
if (NOT DEFINED ENV{BANAN_ARCH})
message(FATAL_ERROR "environment variable BANAN_ARCH not defined")
endif ()
set(BANAN_ARCH $ENV{BANAN_ARCH})
project(banan_os-bootloader-installer CXX)
set(SOURCES
crc32.cpp
ELF.cpp
GPT.cpp
GUID.cpp
main.cpp
)
add_executable(banan_os-bootloader-installer ${SOURCES})
target_compile_options(banan_os-bootloader-installer PRIVATE -O2 -std=c++20)
target_compile_definitions(banan_os-bootloader-installer PRIVATE __arch=${BANAN_ARCH})
target_include_directories(banan_os-bootloader-installer PRIVATE ${CMAKE_SOURCE_DIR}/../../userspace/libraries/LibELF/include)
target_include_directories(banan_os-bootloader-installer PRIVATE ${CMAKE_SOURCE_DIR}/../../kernel/include)

View File

@ -0,0 +1,142 @@
#include "ELF.h"
#include <LibELF/Values.h>
#include <cassert>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <sys/mman.h>
#include <unistd.h>
using namespace LibELF;
ELFFile::ELFFile(std::string_view path)
: m_path(path)
{
m_fd = open(m_path.c_str(), O_RDONLY);
if (m_fd == -1)
{
std::cerr << "Could not open '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
if (fstat(m_fd, &m_stat) == -1)
{
std::cerr << "Could not stat '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
void* mmap_addr = mmap(nullptr, m_stat.st_size, PROT_READ, MAP_PRIVATE, m_fd, 0);
if (mmap_addr == MAP_FAILED)
{
std::cerr << "Could not mmap '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
m_mmap = reinterpret_cast<uint8_t*>(mmap_addr);
if (!validate_elf_header())
return;
m_success = true;
}
ELFFile::~ELFFile()
{
if (m_mmap)
munmap(m_mmap, m_stat.st_size);
m_mmap = nullptr;
if (m_fd != -1)
close(m_fd);
m_fd = -1;
}
const ElfNativeFileHeader& ELFFile::elf_header() const
{
return *reinterpret_cast<LibELF::ElfNativeFileHeader*>(m_mmap);
}
bool ELFFile::validate_elf_header() const
{
if (m_stat.st_size < sizeof(ElfNativeFileHeader))
{
std::cerr << m_path << " is too small to be a ELF executable" << std::endl;
return false;
}
const auto& elf_header = this->elf_header();
if (
elf_header.e_ident[EI_MAG0] != ELFMAG0 ||
elf_header.e_ident[EI_MAG1] != ELFMAG1 ||
elf_header.e_ident[EI_MAG2] != ELFMAG2 ||
elf_header.e_ident[EI_MAG3] != ELFMAG3
)
{
std::cerr << m_path << " doesn't have an ELF magic number" << std::endl;
return false;
}
#if ARCH(x86_64)
if (elf_header.e_ident[EI_CLASS] != ELFCLASS64)
#elif ARCH(i686)
if (elf_header.e_ident[EI_CLASS] != ELFCLASS32)
#endif
{
std::cerr << m_path << " architecture doesn't match" << std::endl;
return false;
}
if (elf_header.e_ident[EI_DATA] != ELFDATA2LSB)
{
std::cerr << m_path << " is not in little endian format" << std::endl;
return false;
}
if (elf_header.e_ident[EI_VERSION] != EV_CURRENT)
{
std::cerr << m_path << " has unsupported version" << std::endl;
return false;
}
if (elf_header.e_type != ET_EXEC)
{
std::cerr << m_path << " is not an executable ELF file" << std::endl;
return false;
}
return true;
}
const ElfNativeSectionHeader& ELFFile::section_header(std::size_t index) const
{
const auto& elf_header = this->elf_header();
assert(index < elf_header.e_shnum);
const uint8_t* section_array_start = m_mmap + elf_header.e_shoff;
return *reinterpret_cast<const ElfNativeSectionHeader*>(section_array_start + index * elf_header.e_shentsize);
}
std::string_view ELFFile::section_name(const ElfNativeSectionHeader& section_header) const
{
const auto& elf_header = this->elf_header();
assert(elf_header.e_shstrndx != SHN_UNDEF);
const auto& section_string_table = this->section_header(elf_header.e_shstrndx);
const char* string_table_start = reinterpret_cast<const char*>(m_mmap + section_string_table.sh_offset);
return string_table_start + section_header.sh_name;
}
std::optional<std::span<const uint8_t>> ELFFile::find_section(std::string_view name) const
{
const auto& elf_header = this->elf_header();
for (std::size_t i = 0; i < elf_header.e_shnum; i++)
{
const auto& section_header = this->section_header(i);
auto section_name = this->section_name(section_header);
if (section_name != name)
continue;
return std::span<const uint8_t>(m_mmap + section_header.sh_offset, section_header.sh_size);
}
return {};
}

View File

@ -0,0 +1,36 @@
#pragma once
#include <LibELF/Types.h>
#include <cstdint>
#include <optional>
#include <span>
#include <string_view>
#include <string>
#include <sys/stat.h>
class ELFFile
{
public:
ELFFile(std::string_view path);
~ELFFile();
const LibELF::ElfNativeFileHeader& elf_header() const;
std::optional<std::span<const uint8_t>> find_section(std::string_view name) const;
bool success() const { return m_success; }
std::string_view path() const { return m_path; }
private:
const LibELF::ElfNativeSectionHeader& section_header(std::size_t index) const;
std::string_view section_name(const LibELF::ElfNativeSectionHeader&) const;
bool validate_elf_header() const;
private:
const std::string m_path;
bool m_success { false };
int m_fd { -1 };
struct stat m_stat { };
uint8_t* m_mmap { nullptr };
};

View File

@ -0,0 +1,250 @@
#include "crc32.h"
#include "GPT.h"
#include <cassert>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <sys/mman.h>
#include <unistd.h>
// FIXME: don't assume 512 byte sectors
#define SECTOR_SIZE 512
GPTFile::GPTFile(std::string_view path)
: m_path(path)
{
m_fd = open(m_path.c_str(), O_RDWR);
if (m_fd == -1)
{
std::cerr << "Could not open '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
if (fstat(m_fd, &m_stat) == -1)
{
std::cerr << "Could not stat '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
void* mmap_addr = mmap(nullptr, m_stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (mmap_addr == MAP_FAILED)
{
std::cerr << "Could not mmap '" << m_path << "': " << std::strerror(errno) << std::endl;
return;
}
m_mmap = reinterpret_cast<uint8_t*>(mmap_addr);
if (!validate_gpt_header())
return;
m_success = true;
}
GPTFile::~GPTFile()
{
if (m_mmap)
munmap(m_mmap, m_stat.st_size);
m_mmap = nullptr;
if (m_fd != -1)
close(m_fd);
m_fd = -1;
}
MBR& GPTFile::mbr()
{
return *reinterpret_cast<MBR*>(m_mmap);
}
const GPTHeader& GPTFile::gpt_header() const
{
return *reinterpret_cast<GPTHeader*>(m_mmap + SECTOR_SIZE);
}
bool GPTFile::install_stage1(std::span<const uint8_t> stage1)
{
auto& mbr = this->mbr();
if (stage1.size() > sizeof(mbr.boot_code))
{
std::cerr << m_path << ": can't fit " << stage1.size() << " bytes of boot code in mbr (max is " << sizeof(mbr.boot_code) << ")" << std::endl;
return false;
}
// copy boot code
memcpy(mbr.boot_code, stage1.data(), stage1.size());
// setup mbr
mbr.unique_mbr_disk_signature = 0xdeadbeef;
mbr.unknown = 0;
mbr.signature = 0xAA55;
// setup mbr partition records
mbr.partition_records[0].boot_indicator = 0x00;
mbr.partition_records[0].starting_chs[0] = 0x00;
mbr.partition_records[0].starting_chs[1] = 0x02;
mbr.partition_records[0].starting_chs[2] = 0x00;
mbr.partition_records[0].os_type = 0xEE;
mbr.partition_records[0].ending_chs[0] = 0xFF;
mbr.partition_records[0].ending_chs[1] = 0xFF;
mbr.partition_records[0].ending_chs[2] = 0xFF;
mbr.partition_records[0].starting_lba = 1;
mbr.partition_records[0].size_in_lba = 0xFFFFFFFF;
memset(&mbr.partition_records[1], 0x00, sizeof(MBRPartitionRecord));
memset(&mbr.partition_records[2], 0x00, sizeof(MBRPartitionRecord));
memset(&mbr.partition_records[3], 0x00, sizeof(MBRPartitionRecord));
return true;
}
bool GPTFile::install_stage2(std::span<const uint8_t> stage2, std::span<const uint8_t> data, const GUID& root_partition_guid)
{
if (data.size() < 16)
{
std::cerr << m_path << ": contains invalid .data section, too small for patches" << std::endl;
return false;
}
// find GUID patch offsets
std::size_t disk_guid_offset(-1);
std::size_t part_guid_offset(-1);
for (std::size_t i = 0; i < data.size() - 16; i++)
{
if (memcmp(data.data() + i, "root disk guid ", 16) == 0)
{
if (disk_guid_offset != std::size_t(-1))
{
std::cerr << m_path << ": contains invalid .data section, multiple patchable disk guids" << std::endl;
return false;
}
disk_guid_offset = i;
}
if (memcmp(data.data() + i, "root part guid ", 16) == 0)
{
if (part_guid_offset != std::size_t(-1))
{
std::cerr << m_path << ": contains invalid .data section, multiple patchable partition guids" << std::endl;
return false;
}
part_guid_offset = i;
}
}
if (disk_guid_offset == std::size_t(-1))
{
std::cerr << m_path << ": contains invalid .data section, no patchable disk guid" << std::endl;
return false;
}
if (part_guid_offset == std::size_t(-1))
{
std::cerr << m_path << ": contains invalid .data section, no patchable partition guid" << std::endl;
return false;
}
auto partition = find_partition_with_type(bios_boot_guid);
if (!partition.has_value())
{
std::cerr << m_path << ": could not find partition with type " << bios_boot_guid << std::endl;
return false;
}
const std::size_t partition_size = (partition->ending_lba - partition->starting_lba + 1) * SECTOR_SIZE;
std::size_t data_offset = stage2.size();
if (std::size_t rem = data_offset % 512)
data_offset += 512 - rem;
if (data_offset + data.size() > partition_size)
{
std::cerr << m_path << ": can't fit " << stage2.size() + data.size() << " bytes of data to partition of size " << partition_size << std::endl;
return false;
}
uint8_t* partition_start = m_mmap + partition->starting_lba * SECTOR_SIZE;
memcpy(partition_start, stage2.data(), stage2.size());
memcpy(partition_start + data_offset, data.data(), data.size());
// patch GUIDs
*reinterpret_cast<GUID*>(partition_start + data_offset + disk_guid_offset) = gpt_header().disk_guid;
*reinterpret_cast<GUID*>(partition_start + data_offset + part_guid_offset) = root_partition_guid;
return true;
}
bool GPTFile::install_bootloader(std::span<const uint8_t> stage1, std::span<const uint8_t> stage2, std::span<const uint8_t> data, const GUID& root_partition_guid)
{
if (!find_partition_with_guid(root_partition_guid).has_value())
{
std::cerr << m_path << ": no partition with GUID " << root_partition_guid << std::endl;
return false;
}
if (!install_stage1(stage1))
return false;
if (!install_stage2(stage2, data, root_partition_guid))
return false;
return true;
}
std::optional<GPTPartitionEntry> GPTFile::find_partition_with_guid(const GUID& guid) const
{
const auto& gpt_header = this->gpt_header();
const uint8_t* partition_entry_array_start = m_mmap + gpt_header.partition_entry_lba * SECTOR_SIZE;
for (std::size_t i = 0; i < gpt_header.number_of_partition_entries; i++)
{
const auto& partition_entry = *reinterpret_cast<const GPTPartitionEntry*>(partition_entry_array_start + i * gpt_header.size_of_partition_entry);
if (partition_entry.partition_guid != guid)
continue;
return partition_entry;
}
return {};
}
std::optional<GPTPartitionEntry> GPTFile::find_partition_with_type(const GUID& type_guid) const
{
const auto& gpt_header = this->gpt_header();
const uint8_t* partition_entry_array_start = m_mmap + gpt_header.partition_entry_lba * SECTOR_SIZE;
for (std::size_t i = 0; i < gpt_header.number_of_partition_entries; i++)
{
const auto& partition_entry = *reinterpret_cast<const GPTPartitionEntry*>(partition_entry_array_start + i * gpt_header.size_of_partition_entry);
if (partition_entry.type_guid != type_guid)
continue;
return partition_entry;
}
return {};
}
bool GPTFile::validate_gpt_header() const
{
if (SECTOR_SIZE + m_stat.st_size < sizeof(GPTHeader))
{
std::cerr << m_path << " is too small to have GPT header" << std::endl;
return false;
}
auto gpt_header = this->gpt_header();
if (std::memcmp(gpt_header.signature, "EFI PART", 8) != 0)
{
std::cerr << m_path << " doesn't contain GPT partition header signature" << std::endl;
return false;
}
const uint32_t header_crc32 = gpt_header.header_crc32;
gpt_header.header_crc32 = 0;
if (header_crc32 != crc32_checksum(reinterpret_cast<uint8_t*>(&gpt_header), gpt_header.header_size))
{
std::cerr << m_path << " has non-matching header crc32" << std::endl;
return false;
}
const std::size_t partition_array_size = gpt_header.number_of_partition_entries * gpt_header.size_of_partition_entry;
if (gpt_header.partition_entry_array_crc32 != crc32_checksum(m_mmap + gpt_header.partition_entry_lba * SECTOR_SIZE, partition_array_size))
{
std::cerr << m_path << " has non-matching partition entry crc32" << std::endl;
return false;
}
return true;
}

View File

@ -0,0 +1,91 @@
#pragma once
#include "GUID.h"
#include <cstdint>
#include <optional>
#include <span>
#include <string_view>
#include <string>
#include <sys/stat.h>
struct MBRPartitionRecord
{
uint8_t boot_indicator;
uint8_t starting_chs[3];
uint8_t os_type;
uint8_t ending_chs[3];
uint32_t starting_lba;
uint32_t size_in_lba;
} __attribute__((packed));
struct MBR
{
uint8_t boot_code[440];
uint32_t unique_mbr_disk_signature;
uint16_t unknown;
MBRPartitionRecord partition_records[4];
uint16_t signature;
} __attribute__((packed));
static_assert(sizeof(MBR) == 512);
struct GPTPartitionEntry
{
GUID type_guid;
GUID partition_guid;
uint64_t starting_lba;
uint64_t ending_lba;
uint64_t attributes;
uint16_t name[36];
};
static_assert(sizeof(GPTPartitionEntry) == 128);
struct GPTHeader
{
char signature[8];
uint32_t revision;
uint32_t header_size;
uint32_t header_crc32;
uint32_t reserved;
uint64_t my_lba;
uint64_t alternate_lba;
uint64_t first_usable_lba;
uint64_t last_usable_lba;
GUID disk_guid;
uint64_t partition_entry_lba;
uint32_t number_of_partition_entries;
uint32_t size_of_partition_entry;
uint32_t partition_entry_array_crc32;
} __attribute__((packed));
static_assert(sizeof(GPTHeader) == 92);
class GPTFile
{
public:
GPTFile(std::string_view path);
~GPTFile();
bool install_bootloader(std::span<const uint8_t> stage1, std::span<const uint8_t> stage2, std::span<const uint8_t> data, const GUID& root_partition_guid);
const GPTHeader& gpt_header() const;
bool success() const { return m_success; }
std::string_view path() const { return m_path; }
private:
MBR& mbr();
bool validate_gpt_header() const;
std::optional<GPTPartitionEntry> find_partition_with_guid(const GUID& guid) const;
std::optional<GPTPartitionEntry> find_partition_with_type(const GUID& type_guid) const;
bool install_stage1(std::span<const uint8_t> stage1);
bool install_stage2(std::span<const uint8_t> stage2, std::span<const uint8_t> data, const GUID& root_partition_guid);
private:
const std::string m_path;
bool m_success { false };
int m_fd { -1 };
struct stat m_stat { };
uint8_t* m_mmap { nullptr };
};

View File

@ -0,0 +1,74 @@
#include "GUID.h"
#include <iomanip>
#include <cstring>
std::optional<uint64_t> parse_hex(std::string_view hex_string)
{
uint64_t result = 0;
for (char c : hex_string)
{
if (!isxdigit(c))
return {};
uint8_t nibble = 0;
if ('0' <= c && c <= '9')
nibble = c - '0';
else if ('a' <= c && c <= 'f')
nibble = c - 'a' + 10;
else
nibble = c - 'A' + 10;
result = (result << 4) | nibble;
}
return result;
}
std::optional<GUID> GUID::from_string(std::string_view guid_string)
{
if (guid_string.size() != 36)
return {};
if (guid_string[8] != '-' || guid_string[13] != '-' || guid_string[18] != '-' || guid_string[23] != '-')
return {};
auto comp1 = parse_hex(guid_string.substr(0, 8));
auto comp2 = parse_hex(guid_string.substr(9, 4));
auto comp3 = parse_hex(guid_string.substr(14, 4));
auto comp4 = parse_hex(guid_string.substr(19, 4));
auto comp5 = parse_hex(guid_string.substr(24, 12));
if (!comp1.has_value() || !comp2.has_value() || !comp3.has_value() || !comp4.has_value() || !comp5.has_value())
return {};
GUID result;
result.component1 = *comp1;
result.component2 = *comp2;
result.component3 = *comp3;
for (int i = 0; i < 2; i++)
result.component45[i + 0] = *comp4 >> ((2-1) * 8 - i * 8);
for (int i = 0; i < 6; i++)
result.component45[i + 2] = *comp5 >> ((6-1) * 8 - i * 8);
return result;
}
bool GUID::operator==(const GUID& other) const
{
return std::memcmp(this, &other, sizeof(GUID)) == 0;
}
std::ostream& operator<<(std::ostream& out, const GUID& guid)
{
auto flags = out.flags();
out << std::hex << std::setfill('0');
out << std::setw(8) << guid.component1 << '-';
out << std::setw(4) << guid.component2 << '-';
out << std::setw(4) << guid.component3 << '-';
out << std::setw(2);
for (int i = 0; i < 2; i++) out << +guid.component45[i];
out << '-';
for (int i = 2; i < 8; i++) out << +guid.component45[i];
out.flags(flags);
return out;
}

View File

@ -0,0 +1,37 @@
#pragma once
#include <cstdint>
#include <optional>
#include <ostream>
#include <string_view>
struct GUID
{
static std::optional<GUID> from_string(std::string_view);
uint32_t component1;
uint16_t component2;
uint16_t component3;
// last 2 components are combined so no packed needed
uint8_t component45[8];
bool operator==(const GUID& other) const;
};
std::ostream& operator<<(std::ostream& out, const GUID& guid);
// unused 00000000-0000-0000-0000-000000000000
static constexpr GUID unused_guid = {
0x00000000,
0x0000,
0x0000,
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
};
// bios boot 21686148-6449-6E6F-744E-656564454649
static constexpr GUID bios_boot_guid = {
0x21686148,
0x6449,
0x6E6F,
{ 0x74, 0x4E, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 }
};

View File

@ -0,0 +1,80 @@
#include "crc32.h"
static constexpr uint32_t crc32_table[256] =
{
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
};
uint32_t crc32_checksum(const uint8_t* data, std::size_t count)
{
uint32_t crc32 = 0xFFFFFFFF;
for (size_t i = 0; i < count; i++)
{
uint8_t index = (crc32 ^ data[i]) & 0xFF;
crc32 = (crc32 >> 8) ^ crc32_table[index];
}
return crc32 ^ 0xFFFFFFFF;
}

View File

@ -0,0 +1,6 @@
#pragma once
#include <cstddef>
#include <cstdint>
uint32_t crc32_checksum(const uint8_t* data, std::size_t count);

View File

@ -0,0 +1,45 @@
#include "ELF.h"
#include "GPT.h"
#include <iostream>
int main(int argc, char** argv)
{
using namespace std::string_view_literals;
if (argc != 4)
{
std::fprintf(stderr, "usage: %s BOOTLOADER DISK_IMAGE ROOT_PARTITION_GUID\n", argv[0]);
return 1;
}
auto root_partition_guid = GUID::from_string(argv[3]);
if (!root_partition_guid.has_value())
{
std::cerr << "invalid guid '" << argv[3] << '\'' << std::endl;
return 1;
}
ELFFile bootloader(argv[1]);
if (!bootloader.success())
return 1;
auto stage1 = bootloader.find_section(".stage1"sv);
auto stage2 = bootloader.find_section(".stage2"sv);
auto data = bootloader.find_section(".data"sv);
if (!stage1.has_value() || !stage2.has_value() || !data.has_value())
{
std::cerr << bootloader.path() << " doesn't contain .stage1, .stage2 and .data sections" << std::endl;
return 1;
}
GPTFile disk_image(argv[2]);
if (!disk_image.success())
return 1;
if (!disk_image.install_bootloader(*stage1, *stage2, *data, *root_partition_guid))
return 1;
std::cout << "bootloader installed" << std::endl;
return 0;
}

1
bos Symbolic link
View File

@ -0,0 +1 @@
script/build.sh

View File

@ -1,7 +0,0 @@
#!/bin/sh
set -e
. ./headers.sh
for PROJECT in $PROJECTS; do
(cd $PROJECT && DESTDIR="$SYSROOT" $MAKE install)
done

View File

@ -1,11 +0,0 @@
#!/bin/sh
set -e
. ./config.sh
for PROJECT in $PROJECTS; do
(cd $PROJECT && $MAKE clean)
done
rm -rf sysroot
rm -rf isodir
rm -rf banan-os.img

View File

@ -1,33 +0,0 @@
SYSTEM_HEADER_PROJECTS="libc BAN kernel"
PROJECTS="libc BAN kernel"
export MAKE=${MAKE:-make}
export HOST=${HOST:-$(./default-host.sh)}
export AR=${HOST}-ar
export AS=${HOST}-as
export CC=${HOST}-gcc
export CXX=${HOST}-g++
export PREFIX=/usr
export EXEC_PREFIX=$PREFIX
export BOOTDIR=/boot
export LIBDIR=$EXEC_PREFIX/lib
export INCLUDEDIR=$PREFIX/include
export CFLAGS='-O2 -g'
export CPPFLAGS='--std=c++20 -Wno-literal-suffix'
export UBSAN=0
# Configure the cross-compiler to use the desired system root.
export SYSROOT="$(pwd)/sysroot"
export CC="$CC --sysroot=$SYSROOT"
export CXX="$CXX --sysroot=$SYSROOT"
# Work around that the -elf gcc targets doesn't have a system include directory
# because it was configured with --without-headers rather than --with-sysroot.
if echo "$HOST" | grep -Eq -- '-elf($|-)'; then
export CC="$CC -isystem=$INCLUDEDIR"
export CXX="$CXX -isystem=$INCLUDEDIR"
fi

View File

@ -1,2 +0,0 @@
#!/bin/sh
echo x86_64-elf

67
disk.sh
View File

@ -1,67 +0,0 @@
#!/bin/sh
set -e
. ./build.sh
DISK_NAME=banan-os.img
DISK_SIZE=$[50 * 1024 * 1024]
MOUNT_DIR=/mnt
dd if=/dev/zero of=$DISK_NAME bs=512 count=$[$DISK_SIZE / 512]
sed -e 's/\s*\([-\+[:alnum:]]*\).*/\1/' << EOF | fdisk $DISK_NAME
g # gpt
n # new partition
1 # partition number 1
# default (from the beginning of the disk)
+1MiB # bios boot partiton size
n # new partition
2 # partition number 2
# default (right after bios boot partition)
# default (to the end of disk)
t # set type
1 # ... of partition 1
4 # bios boot partition
t # set type
2 # ... of partition 2
20 # Linux filesystem
x # expert menu
n # partition name
2 # ... of partition 2
banan-root
r # back to main menu
w # write changes
EOF
LOOP_DEV=$(sudo losetup -f --show $DISK_NAME)
sudo partprobe $LOOP_DEV
PARTITION1=${LOOP_DEV}p1
PARTITION2=${LOOP_DEV}p2
sudo mkfs.ext2 $PARTITION2
sudo mount $PARTITION2 $MOUNT_DIR
sudo cp -r ${SYSROOT}/* ${MOUNT_DIR}/
sudo mkdir -p ${MOUNT_DIR}/usr/share/
sudo cp -r fonts ${MOUNT_DIR}/usr/share/
sudo grub-install --no-floppy --target=i386-pc --modules="normal ext2 multiboot" --boot-directory=${MOUNT_DIR}/boot $LOOP_DEV
echo -e '
menuentry "banan-os" {
multiboot /boot/banan-os.kernel
}
menuentry "banan-os (no serial)" {
multiboot /boot/banan-os.kernel noserial
}
menuentry "banan-os (no apic)" {
multiboot /boot/banan-os.kernel noapic
}
menuentry "banan-os (no apic, no serial)" {
multiboot /boot/banan-os.kernel noapic noserial
}
' | sudo tee ${MOUNT_DIR}/boot/grub/grub.cfg
sudo umount $MOUNT_DIR
sudo losetup -d $LOOP_DEV

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,9 +0,0 @@
#!/bin/sh
set -e
. ./config.sh
mkdir -p "$SYSROOT"
for PROJECT in $SYSTEM_HEADER_PROJECTS; do
(cd $PROJECT && DESTDIR="$SYSROOT" $MAKE install-headers)
done

Some files were not shown because too many files have changed in this diff Show More