Commit Graph

944 Commits

Author SHA1 Message Date
Justin C. Miller
7d5feb943f [kernel] Add handle badge to ctor/assignment
Handles already had a field for badge, but did not touch it in the
constructors or in assignment operators.
2022-02-03 00:02:46 -08:00
Justin C. Miller
b6d4fb698c [kernel] Support handle tag directly on syscalls
The "handle" tag on syscall parameters causes syscall_verify.cpp to pass
the resulting object as a obj::handle* instead of directly as an object
pointer. Now the handle tag is supported directly on the syscall itself
as well, causing the "self" object to be passed as a handle pointer.
2022-02-01 00:39:15 -08:00
Justin C. Miller
e3ecd73cd8 [util] Add constexpr log2/is_pow2 helpers
I didn't end up using these, but constexpr log2 and is_pow2 functions
might be helpful.
2022-01-30 21:02:32 -08:00
Justin C. Miller
5dfc6ae62e [kernel] Add event syscalls
The event object was missing any syscalls. Furthermore, kobject had an
old object_signal implementation (the syscall itself no longer exists),
which was removed. User code should only be able to set signals on
events.
2022-01-30 21:00:46 -08:00
Justin C. Miller
343622d4e5 [kernel] Fix up formatting
Two minor issues: scheduler::prune wasn't formatted correctly, and
j6/caps.h was not using the ull prefix when shifting 64 bit numbers.
(It's doubtful an object would get more than 32 caps any time soon, but
better to be correct.)
2022-01-30 20:52:43 -08:00
Justin C. Miller
9945ebab34 [kernel] Get rid of obsolete thread loading state
The thread::state::loading flag was left over from a time when the
kernel did elf loading for all processes.
2022-01-30 20:50:48 -08:00
Justin C. Miller
42774d94c0 [kernel] Fix SMP not starting
The cpu.cpp/smp.cpp cleanup out of kernel_main missed an important call:
kernel_main never called smp::ready() to unblock the APs waiting for the
scheduler to be ready.
2022-01-30 20:48:50 -08:00
Justin C. Miller
dd535158f2 [kernel] Re-add slab_allocated mixin
The return of slab_allocated! Now after the kutil/util/kernel giant
cleanup, this belongs squarely in the kernel, and works much better
there. Slabs are allocated via a bump pointer into a new kernel VMA,
instead of using kalloc() or allocating pages directly.
2022-01-30 20:46:19 -08:00
Justin C. Miller
a7245116b6 [util] Add util::deque container
Adding the util::deque container, implemented with the util::linked_list
of arrays of items.

Also, use the deque for a kobject's blocked thread list to maintain
order instead of a vector using remove_swap().
2022-01-30 20:42:49 -08:00
Justin C. Miller
2aef7176ab [kernel] Add missing zero_ok changes
This change adds some changes I missed as part of the previous (see
da5c1e9) zero_ok change.
2022-01-30 20:40:51 -08:00
Justin C. Miller
5e1e056623 [tools] Fix gdb j6threads not listing blocked threads
A typo was keeping j6threads from listing the blocked threads list on a
run queue.
2022-01-30 20:34:34 -08:00
Justin C. Miller
da5c1e9833 [kernel] Add new zero_ok flag to syscall params
The new zero_ok flag is similar to optional for reference parameters,
but only in cases where there is a length parameter. If that parameter
is a reference parameter itself and is null, or it is non-null and
contains a non-zero length, or there is no length parameter, then the
main parameter may not be null.
2022-01-30 14:26:36 -08:00
Justin C. Miller
b6218a1121 [kernel] Allow 7+ argument syscalls
The syscall interface is designed to closely follow the System V amd64
calling convention, so that as much as possible, the call into the
assembly trampoline for the syscall sets up the call correctly. Before
this change, the only exception was using r10 (a caller-saved register
already) to stash the contents of rcx, which gets clobbered by the
syscall instruction. However, this only preserves registers for the
function call, as the stack is switched upon kernel entry, and
additional call frames have been added by the time the syscall gets back
into C++ land.

This change adds a new parameter to the syscall in rbx. Since rbx is
callee-saved, the syscall trampoline pushes it to the stack, and then
puts the address of the stack-passed arguments into rbx. Now that the
syscall implementations are wrapped in the _syscall_verify_* functions,
we can piggy-back on those to also set up the extra arguments from the
user stack.

Now, for any syscall with 7 or more arguments, the verify wrapper takes
the first six arguments normally, then gets a stack pointer (the rbx
value) as its 7th and final argument. It's then the job of the verify
wrapper to get the remaining arguments from that stack pointer and pass
them to the implementation function as normal arguments.
2022-01-30 12:25:11 -08:00
Justin C. Miller
17ca402aa0 [build] Rename module ninja files to module.<name>.ninja
I was getting sick of tab completion not working for loading the elf
binaries in gdb, so I'm renaming the module ninja files with a prefix.
2022-01-29 16:05:34 -08:00
Justin C. Miller
cd037aca15 [kernel] Let objects inherit caps from superclasses
The main point of this change is to allow "global" capabilities defined
on the base object type. The example here is the clone capability on all
objects, which governs the ability to clone a handle.

Related changes in this commit:
- Renamed `kobject` to `object` as far as the syscall interface is
  concerned. `kobject` is the cname, but j6_cap_kobject_clone feels
  clunky.
- The above change made me realize that the "object <type>" syntax for
  specifying object references was also clunky, so now it's "ref <type>"
- Having to add `.object` on everywhere to access objects in
  interface.exposes or object.super was cumbersome, so those properties
  now return object types directly, instead of ObjectRef.
- syscall_verify.cpp.cog now generates code to check capabilities on
  handles if they're specified in the definition, even when not passing
  an object to the implementation function.
2022-01-29 15:56:33 -08:00
Justin C. Miller
bdae812274 [kernel] Add handle_clone syscall
Added the handle_clone syscall which allows for cloning a handle with
a subset of the original handle's capabilities.

Related changes:

- srv.init now calls handle_clone on its system handle, and load_program
  was changed to allow this second system handle to be passed to loaded
  programs instead. However, as drv.uart is still a driver AND a log
  reader, this new handle is not actually passed yet.
- The definition parser was using a set for the cap list, which meant
  the order (and thus values) of caps was not static.
- Some code in objects/handle.h was made more explicit about what bits
  meant what.
2022-01-28 23:40:21 -08:00
Justin C. Miller
f1246f84e0 [kernel] Add capabilities to handles
This change finally adds capabilities to handles. Included changes:

- j6_handle_t is now again 64 bits, with the highest 8 bits being a type
  code, and the next highest 24 bits being the capability mask, so that
  programs can check type/caps without calling the kernel.
- The definitions grammar now includes a `capabilities [ ]` section on
  objects, to list what capabilities are relevant.
- j6/caps.h is auto-generated from object capability lists
- init_libj6 again sets __handle_self and __handle_sys, this is a bit
  of a hack.
- A new syscall, j6_handle_list, will return the list of existing
  handles owned by the calling process.
- syscall_verify.cpp.cog now actually checks that the needed
  capabilities exist on handles before allowing the call.
2022-01-28 01:54:45 -08:00
Justin C. Miller
9b75acf0b5 [kernel] Re-add channel syscalls
Channels were unused, and while they were listed in syscalls.def, they
had no syscalls listed in their interface. This change adds them back,
and updates them to the curren syscall style.
2022-01-27 22:37:04 -08:00
Justin C. Miller
42d7f4245d [kernel] Remove placement-new declaration from memory.h.cog
Finishing the trend of using `#include <new>` to define new, get rid of
the last bits of custom-declared operator new.
2022-01-27 22:04:06 -08:00
Justin C. Miller
fd25d3babc [kernel] Clean up main.cpp and others
The kernel/main.cpp and kernel/memory_bootstrap.cpp files had become
something of a junk drawer. This change cleans them up in the following
ways:

- Most CPU initialization has moved to cpu.cpp, allowing several
  functions to be made static and removed from cpu.h
- Multi-core startup code has moved to the new smp.h and smp.cpp, and
  ap_startup.s has been renamed smp.s to match.
- run_constructors() has moved to memory_bootstrap.cpp, and all the
  functionality of that file has been hidden behind a new public
  interface mem::initialize().
- load_init_server() has moved from memory_bootstrap.cpp to main.cpp
2022-01-27 19:28:35 -08:00
Justin C. Miller
3f8dfbd5b2 [kernel] Add locking to endpoint
Endpoints could previously crash if two senders were concurrently
writing to them, so this change adds a spinlock and protects functions
that touch the signals and blocked list.
2022-01-23 19:42:37 -08:00
Justin C. Miller
0394f29f70 [util] Add release() and reaquire() to scoped_lock
Added methods for releasing the lock held in a scoped_lock early, as
well as reacquiring it after. Useful when, eg a thread is about to block
and should not be holding the lock while blocked.
2022-01-23 19:40:00 -08:00
Justin C. Miller
0a07a7af01 [tools] Add qemu.sh options
Changes to qemu.sh:

- Now takes the -l/--log option to create the qemu log in jsix.log,
  otherwise does not ask qemu to log. (Way faster operations.)
- Remove the debug-isa-exit device when starting with the --debug flag,
  so that we can inspect panics
- Changed from 512M to 4G of ram
2022-01-23 19:36:53 -08:00
Justin C. Miller
a30ef5b3dc [build] Update compile_commands.json when building
The compile_commands.json database is used by clangd to understand the
compilation settings of the project. Keep this up to date by making
ninja update it as part of the build, and have it depend on all build
and module files.
2022-01-23 18:05:36 -08:00
Justin C. Miller
2750ec8ef9 [util] Fix bip_buffer concurrency bug
If a bip_buffer's A buffer is in the middle of being appended to, but
that append has not yet been committed, and all committed A data has
been read, the buffer would get into a bad state where m_start_r pointed
to the end of the previous A buffer, but that data is no longer
connected to either A or B. So now we make sure to check m_size_r before
considering A "done".
2022-01-23 17:43:30 -08:00
Justin C. Miller
850999727c Add .cache (from clangd) to gitignore 2022-01-23 00:53:30 -08:00
Justin C. Miller
3dd029b1ac [tools] Add thread state flags to j6threads gdb command
Added a list of currently-set flags on the thread's state. Also stopped
tracebacks and returned instead of erroring out when they threw
gdb.MemoryError.
2022-01-23 00:34:44 -08:00
Justin C. Miller
75e7fe941b [kernel] Fix thread-wait scoped lock scoping bug
A spinlock was recenly added to thread wait states, so that they
couldn't get stuck if their wake event happened while setting the wake
state, and cause the thread to deadlock. But the scoped_lock objects
locking that spinlock were being instantiated as temporaries and
immediately being thrown away because I forgot to name them.
2022-01-23 00:32:06 -08:00
Justin C. Miller
cbd2d9d625 [kernel] Fix scheduler promotion bug
The scheduler was accidentally checking the state of the _currently
running_ thread when seeing if it should promote a thread in the ready
queue. So, ie, constant-priority threads would get promoted as long as
some non-constant-priority thread was the currently-running thread.
2022-01-23 00:29:51 -08:00
Justin C. Miller
1d30322820 [kernel] Pass objects not handles to syscall impls
This commit contains a couple large, interdependent changes:

- In preparation for capability checking, the _syscall_verify_*
  functions now load most handles passed in, and verify that they exist
  and are of the correct type. Lists and out-handles are not converted
  to objects.
- Also in preparation for capability checking, the internal
  representation of handles has changed. j6_handle_t is now 32 bits, and
  a new j6_cap_t (also 32 bits) is added. Handles of a process are now a
  util::map<j6_handle_t, handle> where handle is a new struct containing
  the id, capabilities, and object pointer.
- The kernel object definition DSL gained a few changes to support auto
  generating the handle -> object conversion in the _syscall_verify_*
  functions, mostly knowing the object type, and an optional "cname"
  attribute on objects where their names differ from C++ code.
  (Specifically vma/vm_area)
- Kernel object code and other code under kernel/objects is now in a new
  obj:: namespace, because fuck you <cstdlib> for putting "system" in
  the global namespace. Why even have that header then?
- Kernel object types constructed with the construct_handle helper now
  have a creation_caps static member to declare what capabilities a
  newly created object's handle should have.
2022-01-17 23:23:04 -08:00
Justin C. Miller
e0246df26b [kernel] Add automatic verification to syscalls
Since we have a DSL for specifying syscalls, we can create a verificaton
method for each syscall that can cover most argument (and eventually
capability) verification instead of doing it piecemeal in each syscall
implementation, which can be more error-prone.

Now a new _syscall_verify_* function exists for every syscall, which
calls the real implementation. The syscall table for the syscall handler
now maps to these verify functions.

Other changes:

- Updated the definition grammar to allow options to have a "key:value"
  style, to eventually support capabilities.
- Added an "optional" option for parameters that says a syscall will
  accept a null value.
- Some bonnibel fixes, as definition file changes weren't always
  properly causing updates in the build dep graph.
- The syscall implementation function signatures are no longer exposed
  in syscall.h. Also, the unused syscall enum has been removed.
2022-01-16 15:11:58 -08:00
Justin C. Miller
e845379b1e [kernel] Use the hpet clock source in scheduler
There has been a global clock object for a while now, but scheduler was
never using it, instead still using its simple increment clock. Now it
uses the hpet clock.
2022-01-15 22:31:00 -08:00
Justin C. Miller
c631ec5ef5 [uart] Add first pass UART driver and logger
First attempt at a UART driver. I'm not sure it's the most stable. Now
that userspace is handling displaying logs, also removed serial and log
output support from the kernel.
2022-01-15 18:20:37 -08:00
Justin C. Miller
44d3918e4f [util] Add try_aquire to spinlock
Also added a scoped_trylock class that mirrors scoped_lock, but uses
try_aquire and has a scoped_lock::locked() accessor.
2022-01-15 17:51:55 -08:00
Justin C. Miller
19f9496889 [kernel] Add a timeout to endpoint recieve syscalls
This also required adding support for multiple wait conditions on a
thread, so wait_type is an enum_bitfield now.

I really need a real clock.
2022-01-15 17:48:19 -08:00
Justin C. Miller
2dd78beb92 [tools] Add j6threads gdb command
The j6threads command shows the current thread, ready threads, and
blocked threads for a given CPU.

To support this, TCB structs gained a pointer to their thread (instead
of trying to do offset magic) and threads gained a pointer to their
creator. Also removed thread::from_tcb() now that the TCB has a pointer.
2022-01-15 17:45:12 -08:00
Justin C. Miller
7bb6b21c65 [kernel] Simplify kernel logger
The logger had a lot of code that was due to it being in kutil instead
of the kernel. Simplifying it a bit here in order to make the uart
logger easier and eventual paring down of the logger easier.

- Log areas are no longer hashes of their names, just an enum
- Log level settings are no longer saved in 4 bits, just a byte
- System signal updating is done in the logger now
2022-01-15 10:00:13 -08:00
Justin C. Miller
11eef8d892 [kernel] Add process_give_handle syscall
This syscall allows a process to give another process access to an
object it has a handle to. The value of the handle as seen in the
receiver process is returned to the caller, so that the caller may
notify the recipient which handle was given.
2022-01-15 09:37:55 -08:00
Justin C. Miller
4d9b33ecd4 [panic] Allow assert/panic to take optional user cpu_state
In places where the "user" state is available, like interrupt handlers,
panic() and kassert() can now take an optional pointer to that user
cpu_state structure, and the panic handler will print that out as well.
2022-01-15 09:33:38 -08:00
Justin C. Miller
421fe33dc0 [boot] Scroll serial output before exit_boot_services
The last line of the boot output was always getting cut off by anything
else getting printed to the serial port. Adding two newlines to clear
the cursor of the previous output.
2022-01-15 09:08:46 -08:00
Justin C. Miller
5c82e5ba1b [tools] Fix gdb j6bt & j6stack commands
These commands had a number of issues. They weren't evaluating their
arguments (eg, you couldn't use a symbol name instead of a number), and
they weren't explicitly using hex when evaluating numbers, so they were
getting incorrect values when the default radix was not 10.
2022-01-15 09:07:00 -08:00
Justin C. Miller
b3aaddadc8 [kernel] Expose a sysconf page to userspace
A structure, system_config, which is dynamically defined by the
definitions/sysconf.yaml config, is now mapped into every user address
space. The kernel fills this with information about itself and the
running machine.

User programs access this through the new j6_sysconf fake syscall in
libj6.

See: Github bug #242
See: [frobozz blog post](https://jsix.dev/posts/frobozz/)

Tags:
2022-01-13 22:08:35 -08:00
Justin C. Miller
939022bb5e [build] Change memory_layout from csv to yaml
I realized we don't need yet another format for configuration. As a
bonus, yaml also allows for a more descriptive file.
2022-01-13 20:23:14 -08:00
Justin C. Miller
950360fddc [libj6] Move remaining j6 headers out of src/include
This means the kernel now depends on libj6. I've added the macro
definition __j6kernel when building for the kernel target, so I can
remove parts with #ifdefs.
2022-01-12 16:04:16 -08:00
Justin C. Miller
2ff7a0864b [build] Fix handling of cog header deps
The last commit was a bandaid, but this needed a real fix. Now we create
a .parse_deps.phony file in every module build dir that implicitly
depends on that module's dependencies' .parse_deps.phony files, as well
as order-only depends on any cog-parsed output for that module. This
causes the cog files to get generated first if they never have been, but
still leaves real header dependency tracking to clang's depfile
generation.
2022-01-08 15:34:47 -08:00
Justin C. Miller
488f2df869 [boot] Add explicit dependency on bootproto/memory.h
bootproto/memory.h is generated from a cog file. Some builds could fail
because the dependency on this file was not explicit.
2022-01-08 14:38:54 -08:00
Justin C. Miller
6877944d17 [kernel] Make unknown IRQs a warning, not a panic
There's not a good reason for them to panic the kernel, not even the
traceback in the panic will be useful.
2022-01-08 01:13:55 -08:00
Justin C. Miller
5083d3d13e [tools] Stop using ovmf_vars_d in qemu.sh
ovmf_vars_d is no longer used by the bootloader, stop having qemu.sh
request it.
2022-01-08 01:11:34 -08:00
Justin C. Miller
eeef23c2b7 [panic] Have panics stop all cores
Kernel panics previously only stopped the calling core. This commit
re-implements the panic system to allow us to stop all cores on a panic.

Changes include:

- panic now sends an NMI to all cores. This means we can't control the
  contents of their registers, so panic information has been moved to a
  global struct, and the panicking cpu sets the pointer to that data in
  its cpu_data.
- the panic_handler is now set up with mutexes to print appropriately
  and only initialize objects once.
- copying _current_gsbase into the panic handler, and #including the
  cpprt.cpp file (so that we can define NDEBUG and not have it try to
  link the assert code back in)
- making the symbol data pointer in kargs an actual pointer again, not
  an address - and carrying that through to the panic handler
- the number of cpus is now saved globally in the kernel as g_num_cpus
2022-01-08 01:00:43 -08:00
Justin C. Miller
a3fff889d1 [boot] Create bootconfig to tell boot what to load
While bonnibel already had the concept of a manifest, which controls
what goes into the built disk image, the bootloader still had filenames
hard-coded. Now bonnibel creates a 'jsix_boot.dat' file that tells the
bootloader what it should load.

Changes include:

- Modules have two new fields: location and description. location is
  their intended directory on the EFI boot volume. description is
  self-explanatory, and is used in log messages.
- New class, boot::bootconfig, implements reading of jsix_boot.dat
- New header, bootproto/bootconfig.h, specifies flags used in the
  manifest and jsix_boot.dat
- New python module, bonnibel/manifest.py, encapsulates reading of the
  manifest and writing jsix_boot.dat
- Syntax of the manifest changed slightly, including adding flags
- Boot and Kernel target ccflags unified a bit (this was partly due to
  trying to get enum_bitfields to work in boot)
- util::counted gained operator+= and new free function util::read<T>
2022-01-07 22:43:44 -08:00