Commit Graph

280 Commits

Author SHA1 Message Date
b035795eb3 Kernel: Move errno.h and (k)assert.h out of the main include directory
This is mostly so IDEs don't pick them up instead of the userspace headers :)
2022-10-19 17:41:23 +02:00
aa90e4a8d9 libc: Implement freopen() 2022-10-19 17:32:59 +02:00
671f2a2de3 Kernel, libc: Implement waitpid()
FIXME: exec() is now doing weird page table stuff. But at least it works, no panics :)
2022-10-18 21:30:52 +02:00
f1bfa6bec8 Build system: more cflags + stripping 2022-10-18 18:28:28 +02:00
01564cb905 libc: Adapt libc to getprocid() + add getppid() 2022-10-18 17:36:33 +02:00
3c1146f2c5 libc: Implement setjmp() and longjmp() 2022-10-17 21:22:18 +02:00
511ad67a9a fdopen: Check for malloc errors 2022-10-17 20:54:32 +02:00
e17a21dbad libc: Use fdopen() after calling open() in fopen() 2022-10-17 20:54:09 +02:00
8b17065718 Kernel, libc: Rename gettid() to getpid() and move it to unistd.h 2022-10-17 20:08:44 +02:00
87ef210759 Kernel, libc: Remove spawn()
Now, fork() and exec() are both implemented. More POSIX-y, thus spawn can be removed.
2022-10-17 19:55:01 +02:00
e7522c21ca libc: Document fork() 2022-10-17 18:56:04 +02:00
64f5078494 Kernel, libc: Implement fork()
This time for real.

Also, add a new per-user-task virtual address allocator (UserHeap), so that mmap'ed pages are in user range and can be copied.
2022-10-17 18:43:35 +02:00
9b39d618de Kernel, libc: Implement spawn()
This function is a Luna alternative to fork() and exec().

Why? Simply because I can't figure out for the life of me how to implement a working fork().

So meanwhile, we have spawn() as a replacement. exec() still exists, though.
2022-10-16 18:48:35 +02:00
6d821d2638 libc: Add support for mkdir() 2022-10-16 17:37:29 +02:00
f35bbe52b7 fnctl: Do not switch on cmd 2022-10-16 17:27:15 +02:00
e1e86ab889 libc: Add support for EEXIST, ENOTDIR, ENOSPC, ENOTSUP and EPIPE to strerror() 2022-10-16 17:24:58 +02:00
d2e2883a79 Kernel: Make mkdir() accessible to userspace 2022-10-16 17:22:12 +02:00
42b6b927c9 libc: Implement strlcpy(), and make strncpy() standard-compliant 2022-10-15 17:24:22 +02:00
48f38bdcef libc: More inttypes.h 2022-10-15 16:53:26 +02:00
45afd3e243 Replace the rand() which reads from /dev/random with a Mersenne PRNG.
Much better now.

Also, it is seeded by reading from /dev/random at startup. Not sure if that's the best idea, but we'll see.
2022-10-15 16:46:54 +02:00
18f1f8b7ca Add VERY HACKY rand() implementation. 2022-10-15 16:31:08 +02:00
dd358eca29 libc/libgen.cpp: Remove #include <luna.h>
This header was included for the NOT_IMPLEMENTED macro.

Now it is implemented.
2022-10-15 16:09:54 +02:00
116e7326a4 libc: Implement dirname() and basename() 2022-10-15 16:08:27 +02:00
5256166e7a libc: Fix strrchr 2022-10-15 16:06:41 +02:00
6953a28ce8 libc: Implement strrchr() 2022-10-15 15:16:19 +02:00
f13c48b562 libc: make strerror() return "Unknown error" instead of an invalid pointer 2022-10-15 15:14:30 +02:00
7139b4403f libc: Add EPIPE (not returned by anything yet) 2022-10-15 15:14:02 +02:00
e3e33bacbc libc: Add stubbed signal.h header 2022-10-15 15:13:38 +02:00
8d552b1522 libc: Add setjmp.h stub functions (not implemented) 2022-10-15 15:13:25 +02:00
46f60b192a libc: Add libgen.h stub functions (not implemented) 2022-10-15 15:13:10 +02:00
9aa96de61d libc: Start implementing inttypes.h 2022-10-15 15:12:53 +02:00
6507146c60 libc: Add Luna-specific limits 2022-10-15 14:31:55 +02:00
f50017912d libc: Implement atoi(), atol() and atoll() 2022-10-15 14:02:24 +02:00
2395c7a871 libc: Implement _Exit
Exactly the same as _exit, but in stdlib.h instead of unistd.h
2022-10-15 14:02:10 +02:00
b1739f7f0d libc: Add support for the new clock() system call 2022-10-15 13:21:22 +02:00
3a9dddaa57 Kernel, libc: Remove the rand() system call
That's why we now have a VFS and a /dev pseudo-filesystem. To provide that kind of things.

Remember, everything is a file!!

The new way to ask the kernel for random numbers is to read from /dev/random.
2022-10-15 13:04:48 +02:00
225284a6ca libc: Add support for mprotect() 2022-10-15 12:59:13 +02:00
613f8170b6 Kernel, libc: Implement mprotect() and use proper PROT_* values 2022-10-15 12:57:14 +02:00
a002e75725 libc: Add strstr() 2022-10-15 12:33:36 +02:00
3fde7e46f5 libc: Add strcmp() and strncmp() 2022-10-15 12:30:49 +02:00
3e2a4276e9 libc: Add memcmp() and memmove()
Those were there since forever in the kernel, but they haven't been added to libc until now.
2022-10-15 12:23:37 +02:00
94a6336e4d libc: add memchr(), strnlen(), strdup(), and rename memclr() to bzero() 2022-10-15 12:18:37 +02:00
523e88e5a9 libc: Add sched_yield() 2022-10-15 11:43:13 +02:00
b0e071e964 libc: Make functions with no arguments be defined as type function(void)
Instead of type function().
2022-10-15 11:39:13 +02:00
3e2bebf0aa libc: Alias __lc_unreachable to __builtin_unreachable 2022-10-15 11:20:20 +02:00
1e86acd4c0 libc: Implement fcntl(F_DUPFD) and dup() 2022-10-15 11:16:34 +02:00
c77e752a82 libc: Implement fileno() 2022-10-15 10:28:52 +02:00
d0d6557e99 libc: Add assert() 2022-10-15 10:05:48 +02:00
8398b2e2e4 libc: Change bits/macros.h to use a __lc_ prefix for internal macros
That way, we don't pollute user programs with our own 'noreturn' and 'deprecated' macros
2022-10-15 09:52:37 +02:00
1043b0772d Make libc exit with a specific code if program initialization fails
Now you know, if a program exits with code -127 it's libc's fault :)
2022-10-14 21:26:46 +02:00
d5bc87099f libc: #define STDOUT_FILENO and STDERR_FILENO
Still missing stdin >.<
2022-10-14 21:24:18 +02:00
eb67ab113e libc: Add ctype.h 2022-10-14 21:12:26 +02:00
ccf8f404a8 libc: Make the stdio initialization code cleaner 2022-10-14 19:36:20 +02:00
4e08c9d8ce libc: Pass the last argument in __luna_syscall5 correctly 2022-10-14 19:24:58 +02:00
0e64c57e10 libc: Change __{stderr, stdout} to {stderr, stdout} 2022-10-14 19:23:14 +02:00
3b83d7ccaf libc: Implement fsetpos() and fgetpos()
Not much to do, since these are kind of equivalent to fseek() and ftell().
2022-10-14 19:12:40 +02:00
743aedcd49 libc: Implement atexit() and _exit()
exit() now calls registered handlers before calling _exit().

And initialize_libc() can now register a handler to close stdout and stderr on program termination!! :)
2022-10-12 20:41:55 +02:00
be9026442e libc: Check for file descriptors 0 and 1, and if they exist do not close and reopen them 2022-10-12 20:19:45 +02:00
de6041fede libc: Add fdopen() 2022-10-12 20:19:13 +02:00
5f8376409d Kernel, libc: Implement EFAULT 2022-10-12 19:25:35 +02:00
4091799701 Kernel, libc: Add ENOEXEC (Exec format error) 2022-10-12 19:15:44 +02:00
531afc3d6f libc: Add support for the new exec() system call
execv() is a temporary wrapper that ignores the second parameter, while execve() and execvp() still error out.
2022-10-12 17:45:58 +02:00
34c35163e4 libc: use the normal names that everybody uses for stdout and stderr 2022-10-12 17:15:57 +02:00
dfdc3b2d11 libc: close fds 0 and 1 before opening stdout and stderr 2022-10-12 17:14:49 +02:00
edda41a7bb libc: Implement fseek(), ftell() and rewind()
All three use the new syscall seek() (with its lseek() wrapper in unistd.h)!!
2022-10-12 15:56:03 +02:00
4a5db1dca7 libc: Add lseek() 2022-10-12 15:37:29 +02:00
928ade123c libc: Add support for the new seek() system call 2022-10-12 15:32:09 +02:00
e40304f2f1 libc: Add off_t to sys/types.h 2022-10-12 15:30:41 +02:00
e90b90c556 Kernel, libc: Round up to nearest page-aligned size instead of down 2022-10-12 12:15:12 +02:00
d89685bb36 libc: Document sys/types.h 2022-10-12 12:07:42 +02:00
1e16a78106 libc: Document functions in sys/mman.h 2022-10-12 12:06:45 +02:00
a3362429d3 libc: Update strerror() 2022-10-12 12:01:18 +02:00
0d3e7d4463 libc: Document errno.h 2022-10-12 12:01:07 +02:00
b42c866db8 Correct description for fopen() 2022-10-12 11:58:07 +02:00
9f5b3b76d2 libc: Document the function in fcntl.h 2022-10-12 11:57:49 +02:00
f44411aa46 libc: Document the functions in luna.h 2022-10-12 11:54:29 +02:00
6aabe19fb4 libc: Document the functions in stdio.h 2022-10-12 11:51:32 +02:00
ffc223c2cf libc: Document functions in stdlib.h
Also, add prototypes for calloc() and realloc(), which were already implemented but not in the header.
2022-10-12 11:30:21 +02:00
c8c4d31cca Add a FIXME to string.cpp 2022-10-12 11:20:48 +02:00
19ee20b6f5 libc: Document the functions in string.h 2022-10-12 11:19:14 +02:00
38e87d8f8f libc: Document the functions in unistd.h 2022-10-12 11:02:18 +02:00
44834b8a0f libc: Implement fputs, fputc, putc and putchar 2022-10-12 10:05:14 +02:00
d3ac590e24 Kernel: Remove the sys_getversion() syscall
User programs can now acquire this information by reading /dev/version.
2022-10-11 21:31:28 +02:00
c30041b733 fix naming 2022-10-11 21:17:07 +02:00
88a01fcfc7 libc: make perror output to stderr 2022-10-11 21:13:38 +02:00
b67011c626 libc: Use the new write() syscall
The new one is write(fd, buf, count).
The old one was write(buf, count).

So the old one tries to pass buf as a file descriptor, and write() complains that 4000000 is too large of a file descriptor and throws EBADF.

We now use the new syscall, through the wrapper that fwrite() provides us.
2022-10-11 21:12:19 +02:00
0f47f59364 libc: make fprintf actually write to the chosen file
Also, printf now is kind of an alias for fprintf(stdout,...), as it should be.
2022-10-11 21:10:19 +02:00
2f46e46aa4 libc: Implement fwrite()
Now that we have the write() syscall and libc wrapper, fwrite can finally be implemented.
2022-10-11 21:09:30 +02:00
80ab982fe4 libc: make stdout and stderr functional
what were before one extern FILE* without reference now are opened by libc on program initialization, to point to /dev/console by default.
2022-10-11 21:08:46 +02:00
53a4b3b85e libc: Add new flags to open()
Since we now have write support, we can add O_WRONLY and O_RDWR to fcntl.h :)
2022-10-11 21:07:21 +02:00
12cf37d0a7 Kernel/syscalls: Modify sys_write to accept a file descriptor and write to it
Previously, sys_write only wrote to the tty. Now, it uses the VFS interface, as it should.
2022-10-11 21:06:12 +02:00
feb8c1c31b libc: Implement strchr() 2022-10-11 19:50:18 +02:00
1278cec065 VFS: Add a 'type' flag to Nodes, implement EISDIR 2022-10-11 17:48:11 +02:00
6c51477197 libc: Implement ferror() and feof() 2022-10-11 16:57:08 +02:00
4f2b3ce5d1 fclose: restore errno after call to free() if close() fails 2022-10-10 21:18:24 +02:00
93f6be9319 libc: Implement the start of a FILE* API (the standard, portable C way of doing file stuff) 2022-10-10 21:08:57 +02:00
9e0bd39964 libc: Implement wrappers for sys_{open,read,write}
read() and close() are in unistd.h, but open() in fnctl.h.
I thought only the definitions for O_SOMETHING were in fnctl.h, but it is as it is.
Don't know why, but let's not anger the Unix gods.

The FILE* C API is pending as well.
2022-10-10 20:45:26 +02:00
da2ede3450 Kernel, libc, userspace: Implement file descriptors
Kernel: Implement a descriptor struct which stores the opened node and read offset, and give each task 8 of those.
Implement three syscalls: sys_read, sys_open and sys_close (sys_write still writes to the console instead of using a fd, for now)
Implement three new errors: ENOENT, EBADF and EMFILE.

libc: Implement the new errors, and the new syscalls in syscall().
Also fix _RETURN_WITH_ERRNO() to set errno correctly, which was making strerror() return null, thus crashing perror().

userspace: make init demonstrate the new file API.
2022-10-10 20:21:39 +02:00
49c7900407 Add %m to userspace printf
%m as a format specifier is a nonstandard glibc extension, but I like it so I'm implementing it.
What it does is print the value of strerror(errno), without consuming any arguments to printf().
2022-10-08 18:44:14 +02:00
3ee1f34bc4 Forgot to add break :)
And that, is why you test before pushing and commiting >.<
2022-10-08 18:08:50 +02:00
c67079dd74 Kernel, libc: Implement %p in *printf()
So we can avoid writing (unsigned long)ptr or (uint64_t)ptr everywhere when wanting to print a pointer.
2022-10-08 18:07:33 +02:00
f83a6ace51 Kernel, libc: Add support for providing a status code to exit()
The exit() libc function already accepted an integer, but didn't pass it on to the kernel since we had no mechanism for it to do that.
Now, the kernel stores a task's exit status to display it later (and in the future, return it to userspace via wait()/waitpid())
2022-10-08 17:56:40 +02:00
abcf1b6118 Define PAGE_SIZE as 4096 and use it everywhere instead of using 4096 as a magic number 2022-10-08 14:52:28 +02:00
1235ce8b32 Avoid magic numbers 2022-10-08 14:44:48 +02:00
ce6ec3585c Kernel, libc: Add ENOSYS
This error is returned by the kernel/C Library when an attempt is made to use a system call that doesn't exist.
2022-10-08 14:18:25 +02:00
ac72d64490 Make (v)fprintf alias to (v)printf instead of throwing an error
We don't have files :) (yet)
But if someone wants to fprintf(stderr), then fine. Do it. Except it won't be any different from fprintf(stdout) or printf().
2022-10-08 13:45:57 +02:00
ee7558a9b7 Add a perror() function 2022-10-08 12:42:25 +02:00
8f0b6d80b2 libc: Implement strerror() 2022-10-08 12:29:06 +02:00
71e15e94af Kernel, libc and userspace: Add basic errno support.
Kernel: Add an errno.h header with definitions for each header,
and return those, negated, from syscalls when there is an error.
mmap() returns an invalid address with errno encoded, instead of
returning a negated errno; this address is encoded as ffffffffffffffEE
where EE is errno in hex.

libc: make syscall() return -1 and set errno on error, instead of
returning the raw return value of the system call. Also, add mmap()
and munmap() wrappers in sys/mman.h :).

userspace: make the memeater program show the value of errno
when allocating memory fails.

Things to improve: add perror() and strerror() to make the errno
experience even better! >.<
2022-10-08 12:06:09 +02:00
ad115e9bab libc: Correct include guards 2022-10-08 11:32:01 +02:00
028a1b1a3c libc: Enable even more warnings 2022-10-07 18:19:06 +02:00
952d8fa294 Be more strict with warnings 2022-10-05 17:34:22 +02:00
560b0a1705 libc: Rename the _ folder to bits, as used internally by libraries 2022-10-04 19:11:54 +02:00
48b858af5a libc: Add strncpy and strncat, and deprecate strcpy and strcat (which, since we're building with -Werror, is an instant ban from using these functions) 2022-10-04 19:08:59 +02:00
a050ed9133 libc: add an internal macros.h header with headers to make attributes less verbose 2022-10-04 19:06:56 +02:00
c6ed8d2abb libc: remove duplicate declaration of strcpy 2022-10-04 18:59:13 +02:00
885e39f60f libc: fix printf to actually print correct more-than-one-digit numbers (hint: the bugfix was a very stupid one) 2022-10-03 20:30:12 +02:00
cb60e418b2 Add vprintf, (v)sprintf and (v)snprintf to libc + move the non v-printfs to stdio.cpp, since they can now call their v-variants and thus don't depend on internal_printf 2022-10-03 19:59:33 +02:00
9420484c9b Do not use __builtin_alloca in puts (could overflow the stack for large strings) 2022-10-03 19:05:04 +02:00
34aa953dbc Add printf() and puts(), quite primitive, looks like hex printing isn't there yet... 2022-10-03 19:00:10 +02:00
dae2ff8d50 Add mmap and munmap syscalls, and thus, add malloc and free to libc!! (yet again, thanks to the wonderful liballoc) 2022-10-02 20:45:04 +02:00
db9e1ba17c Add a memclr() SSE-optimized function 2022-10-02 19:13:33 +02:00
1ecd24f5d6 Kernel: Add SSE support (enable SSE on boot and save context (user tasks only) on task switch) 2022-10-02 18:53:54 +02:00
ae95a528f2 Make function stubs in libc loudly abort instead of silently failing 2022-10-02 18:10:53 +02:00
8e6741ebd6 Rename SYS_version to SYS_getversion 2022-10-02 17:25:56 +02:00
3c6c94adda Add a proper syscall() function to unistd.h 2022-10-02 17:02:15 +02:00
6bacded5cf Update build system 2022-10-02 17:01:30 +02:00
f8a72f15c8 Adjust build system to be more flexible and to work with the new toolchain 2022-10-02 12:44:32 +02:00
dc0fd428d3 Syscalls return a value, right? 2022-10-02 10:47:59 +02:00
fcf5923cc0 WIP: Add a C Library, let's try to compile a Hosted GCC cross-compiler using this! 2022-10-01 20:59:22 +02:00