Files
jsix/src/kernel/scheduler.h
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

103 lines
2.7 KiB
C++

#pragma once
/// \file scheduler.h
/// The task scheduler and related definitions
#include <stdint.h>
#include <util/vector.h>
namespace kernel {
namespace args {
struct program;
}}
struct cpu_data;
class lapic;
struct page_table;
struct run_queue;
namespace obj {
class process;
class thread;
}
/// The task scheduler
class scheduler
{
public:
/// Total number of priority levels
static const uint8_t num_priorities = 8;
/// Maximum (least urgent/interactive) priority
static const uint8_t max_priority = num_priorities - 1;
/// Default priority on process creation
static const uint8_t default_priority = 1;
/// Lowest (most urgent) priority achieved via promotion
static const uint8_t promote_limit = 1;
/// How long the base timer quantum is, in us
static const uint64_t quantum_micros = 500;
/// How many quanta a process gets before being rescheduled
static const uint16_t process_quanta = 10;
/// Constructor.
/// \arg cpus The number of CPUs to schedule for
scheduler(unsigned cpus);
~scheduler();
/// Create a new process from a program image in memory.
/// \arg program The descriptor of the pogram in memory
/// \returns The main thread of the loaded process
obj::thread * load_process(kernel::args::program &program);
/// Create a new kernel task
/// \arg proc Function to run as a kernel task
/// \arg priority Priority to start the process with
/// \arg constant True if this task cannot be promoted/demoted
void create_kernel_task(
void (*task)(),
uint8_t priority,
bool constant = false);
/// Get the quantum for a given priority.
static uint32_t quantum(int priority);
/// Start the scheduler working. This may involve starting
/// timer interrupts or other preemption methods.
void start();
/// Run the scheduler, possibly switching to a new task
void schedule();
/// Start scheduling a new thread.
/// \arg t The new thread's TCB
void add_thread(TCB *t);
/// Get a reference to the scheduler
/// \returns A reference to the global system scheduler
static scheduler & get() { return *s_instance; }
private:
friend class obj::process;
static constexpr uint64_t promote_frequency = 10;
static constexpr uint64_t steal_frequency = 10;
void prune(run_queue &queue, uint64_t now);
void check_promotions(run_queue &queue, uint64_t now);
void steal_work(cpu_data &cpu);
uint32_t m_next_pid;
uint32_t m_tick_count;
obj::process *m_kernel_process;
util::vector<run_queue> m_run_queues;
unsigned m_steal_turn = 0;
static scheduler *s_instance;
};