Files
jsix_import/src/kernel/syscalls/system.cpp
Justin C. Miller f7ae2e2220 [kernel] Re-design thread blocking
In preparation for the new mailbox IPC model, blocking threads needed an
overhaul. The `wait_on_*` and `wake_on_*` methods are gone, and the
`block()` and `wake()` calls on threads now pass a value between the
waker and the blocked thread.

As part of this change, the concept of signals on the base kobject class
was removed, along with the queue of blocked threads waiting on any
given object. Signals are now exclusively the domain of the event object
type, and the new wait_queue utility class helps manage waiting threads
when an object does actually need this functionality. In some cases (eg,
logger) an event object is used instead of the lower-level wait_queue.

Since this change has a lot of ramifications, this large commit includes
the following additional changes:

- The j6_object_wait, j6_object_wait_many, and j6_thread_pause syscalls
  have been removed.
- The j6_event_clear syscall has been removed - events are "cleared" by
  reading them now. A new j6_event_wait syscall has been added to read
  events.
- The generic close() method on kobject has been removed.
- The on_no_handles() method on kobject now deletes the object by
  default, and needs to be overridden by classes that should not be.
- The j6_system_bind_irq syscall now takes an event handle, as well as a
  signal that the IRQ should set on the event. IRQs will cause a waiting
  thread to be woken with the appropriate bit set.
- Threads waking due to timeout is simplified to just having a
  wake_timeout() accessor that returns a timestamp.
- The new wait_queue uses util::deque, which caused the disovery of two
  bugs in the deque implementation: empty deques could still have a
  single array allocated and thus return true for empty(), and new
  arrays getting allocated were not being zeroed first.
- Exposed a new erase() method on util::map that takes a node pointer
  instead of a key, skipping lookup.
2022-02-22 00:00:15 -08:00

91 lines
2.2 KiB
C++

#include <j6/errors.h>
#include <j6/types.h>
#include "cpu.h"
#include "device_manager.h"
#include "frame_allocator.h"
#include "logger.h"
#include "memory.h"
#include "objects/event.h"
#include "objects/thread.h"
#include "objects/system.h"
#include "objects/vm_area.h"
#include "syscalls/helpers.h"
extern log::logger &g_logger;
using namespace obj;
namespace syscalls {
using system = class ::system;
j6_status_t
log(const char *message)
{
thread &th = thread::current();
log::info(logs::syscall, "Message[%llx]: %s", th.koid(), message);
return j6_status_ok;
}
j6_status_t
noop()
{
thread &th = thread::current();
log::debug(logs::syscall, "Thread %llx called noop syscall.", th.koid());
return j6_status_ok;
}
[[ noreturn ]] j6_status_t
test_finish(uint32_t exit_code)
{
// Tell QEMU to exit
asm ( "out %0, %1" :: "a"(exit_code+1), "Nd"(0xf4) );
while (1) asm ("hlt");
}
j6_status_t
system_get_log(system *self, void *buffer, size_t *buffer_len)
{
size_t orig_size = *buffer_len;
*buffer_len = g_logger.get_entry(buffer, *buffer_len);
return (*buffer_len > orig_size) ? j6_err_insufficient : j6_status_ok;
}
j6_status_t
system_bind_irq(system *self, event *dest, unsigned irq, unsigned signal)
{
if (device_manager::get().bind_irq(irq, dest, signal))
return j6_status_ok;
return j6_err_invalid_arg;
}
j6_status_t
system_map_phys(system *self, j6_handle_t * area, uintptr_t phys, size_t size, uint32_t flags)
{
// TODO: check to see if frames are already used? How would that collide with
// the bootloader's allocated pages already being marked used?
if (!(flags & vm_flags::mmio))
frame_allocator::get().used(phys, mem::page_count(size));
vm_flags vmf = (static_cast<vm_flags>(flags) & vm_flags::driver_mask);
construct_handle<vm_area_fixed>(area, phys, size, vmf);
return j6_status_ok;
}
j6_status_t
system_request_iopl(system *self, unsigned iopl)
{
if (iopl != 0 && iopl != 3)
return j6_err_invalid_arg;
constexpr uint64_t mask = 3 << 12;
cpu_data &cpu = current_cpu();
cpu.rflags3 = (cpu.rflags3 & ~mask) | ((iopl << 12) & mask);
return j6_status_ok;
}
} // namespace syscalls