mirror of
https://github.com/rdmsr/zag.git
synced 2026-11-01 15:48:21 +00:00
rtl: add LinkerSet
This commit is contained in:
+3
-9
@@ -5,22 +5,16 @@ const r = @import("root");
|
||||
const ke = r.ke;
|
||||
const ki = r.ke.private;
|
||||
|
||||
extern var __init_array_percpu_start: u8;
|
||||
extern var __init_array_percpu_end: u8;
|
||||
|
||||
const id = CpuLocal(u32, 0);
|
||||
|
||||
/// Initialize a CPU. Must be called on all CPUs.
|
||||
pub fn init_cpu(cpu_id: u32) void {
|
||||
const start = @intFromPtr(&__init_array_percpu_start);
|
||||
const end = @intFromPtr(&__init_array_percpu_end);
|
||||
const count = (end - start) / @sizeOf(*const fn () void);
|
||||
const funcs: [*]const *const fn () callconv(.c) void = @ptrFromInt(start);
|
||||
const elems = r.percpu_init_set.elems();
|
||||
|
||||
id.local().* = cpu_id;
|
||||
|
||||
for (0..count) |i| {
|
||||
funcs[i]();
|
||||
for (elems) |func| {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-3
@@ -40,12 +40,14 @@ const pcpu = ke.CpuLocal(PerCpu, .{
|
||||
.queue = undefined,
|
||||
});
|
||||
|
||||
export const dpc_percpu_init linksection(r.percpu_init) = &pcpu_init;
|
||||
|
||||
fn pcpu_init() linksection(r.init) callconv(.c) void {
|
||||
fn pcpu_init() linksection(r.init) void {
|
||||
pcpu.local().queue.init();
|
||||
}
|
||||
|
||||
comptime {
|
||||
_ = r.percpu_init_set.insert(&pcpu_init);
|
||||
}
|
||||
|
||||
/// Enqueue a DPC.
|
||||
/// If the DPC is already enqueued, this function is a no-op.
|
||||
pub fn enqueue(dpc: *Dpc, arg: ?*anyopaque) void {
|
||||
|
||||
+5
-3
@@ -384,10 +384,8 @@ pub fn update_priority_locked(td: *ke.Thread, new_prio: u8) void {
|
||||
|
||||
}
|
||||
|
||||
export const sched_percpu_init linksection(r.percpu_init) = &init_cpu;
|
||||
|
||||
/// Initialize a CPU for use by the scheduler.
|
||||
fn init_cpu() linksection(r.init) callconv(.c) void {
|
||||
fn init_cpu() linksection(r.init) void {
|
||||
var cpu = percpu.local();
|
||||
|
||||
cpu.* = .{
|
||||
@@ -421,6 +419,10 @@ fn init_cpu() linksection(r.init) callconv(.c) void {
|
||||
}
|
||||
}
|
||||
|
||||
comptime {
|
||||
_ = r.percpu_init_set.insert(&init_cpu);
|
||||
}
|
||||
|
||||
/// Called on CPU 0 to initialize load balancing mechanisms.
|
||||
pub fn late_init() linksection(r.init) void {
|
||||
balance_timer.init();
|
||||
|
||||
@@ -49,9 +49,7 @@ var sync_addr: r.VAddr = 0;
|
||||
var sync_count: usize = 0;
|
||||
var shootdown_lock: ke.SpinLock = .init();
|
||||
|
||||
export const tlb_percpu_init linksection(r.percpu_init) = &pcpu_init;
|
||||
|
||||
fn pcpu_init() linksection(r.init) callconv(.c) void {
|
||||
fn pcpu_init() linksection(r.init) void {
|
||||
const local = percpu.local();
|
||||
|
||||
local.valid_states = @splat(.init(0));
|
||||
@@ -59,6 +57,10 @@ fn pcpu_init() linksection(r.init) callconv(.c) void {
|
||||
local.states = @splat(.{ .base = 0, .npages = 0, .state = .init(slot_free), .payload = undefined, .link = undefined });
|
||||
}
|
||||
|
||||
comptime {
|
||||
_ = r.percpu_init_set.insert(&pcpu_init);
|
||||
}
|
||||
|
||||
/// Allocate an invalidation state slot on this CPU.
|
||||
/// O(N) algorithm but N is tiny.
|
||||
fn allocate_slot(cpu: *PerCpu) ?usize {
|
||||
|
||||
@@ -124,10 +124,8 @@ const PerCpu = struct {
|
||||
|
||||
const pcpu = ke.CpuLocal(PerCpu, undefined);
|
||||
|
||||
export const qspinlock_percpu_init linksection(r.percpu_init) = &init_cpu;
|
||||
|
||||
/// Initialize a CPU's MCS nodes.
|
||||
fn init_cpu() linksection(r.init) callconv(.c) void {
|
||||
fn init_cpu() linksection(r.init) void {
|
||||
var cpu = pcpu.local();
|
||||
cpu.curr_idx = 0;
|
||||
|
||||
@@ -137,6 +135,10 @@ fn init_cpu() linksection(r.init) callconv(.c) void {
|
||||
}
|
||||
}
|
||||
|
||||
comptime {
|
||||
_ = r.percpu_init_set.insert(&init_cpu);
|
||||
}
|
||||
|
||||
/// Queued spin lock implementation.
|
||||
/// See https://rdmsr.github.io/writing/qspinlocks
|
||||
pub const QSpinLock = LockTemplate(struct {
|
||||
|
||||
+23
-19
@@ -5,6 +5,8 @@ const rtl = @import("rtl");
|
||||
const r = @import("root");
|
||||
const pl = r.pl;
|
||||
|
||||
const set = rtl.LinkerSet("tunables", *const Entry);
|
||||
|
||||
const Entry = struct {
|
||||
name: []const u8,
|
||||
address: *anyopaque,
|
||||
@@ -18,7 +20,7 @@ pub fn Tunable(comptime T: type, comptime default: T, comptime name: []const u8)
|
||||
return struct {
|
||||
var storage: T = default;
|
||||
|
||||
const metadata: Entry linksection(".data.tunable") =
|
||||
const metadata: Entry =
|
||||
.{
|
||||
.name = name,
|
||||
.address = &storage,
|
||||
@@ -30,33 +32,32 @@ pub fn Tunable(comptime T: type, comptime default: T, comptime name: []const u8)
|
||||
},
|
||||
};
|
||||
|
||||
comptime {
|
||||
_ = set.insert(&metadata);
|
||||
}
|
||||
|
||||
pub fn load() T {
|
||||
_ = metadata;
|
||||
return storage;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
extern var __tunables_start: u8;
|
||||
extern var __tunables_end: u8;
|
||||
|
||||
pub fn init(boot_info: *pl.BootInfo) void {
|
||||
const cmdline = boot_info.cmdline orelse return;
|
||||
|
||||
const start = @intFromPtr(&__tunables_start);
|
||||
const end = @intFromPtr(&__tunables_end);
|
||||
const count = (end - start) / @sizeOf(Entry);
|
||||
const entries: [*]Entry = @ptrFromInt(start);
|
||||
const elems = set.elems();
|
||||
|
||||
for (0..count) |i| {
|
||||
switch (entries[i].type) {
|
||||
for (elems) |entry| {
|
||||
switch (entry.type) {
|
||||
.num => |info| {
|
||||
const a = entries[i].address;
|
||||
const a = entry.address;
|
||||
|
||||
switch (info.signedness) {
|
||||
.unsigned => {
|
||||
const value = rtl.cmdline.get_number(u64, cmdline, entries[i].name) orelse {
|
||||
std.log.warn("Invalid number value for '{s}', falling back.", .{entries[i].name});
|
||||
const value = rtl.cmdline.get_number(u64, cmdline, entry.name) catch |e| {
|
||||
if (e == error.Format) {
|
||||
std.log.warn("Invalid number value for '{s}', falling back.", .{entry.name});
|
||||
}
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -70,8 +71,11 @@ pub fn init(boot_info: *pl.BootInfo) void {
|
||||
},
|
||||
|
||||
.signed => {
|
||||
const value = rtl.cmdline.get_number(i64, cmdline, entries[i].name) orelse {
|
||||
std.log.warn("Invalid number value for '{s}', falling back.", .{entries[i].name});
|
||||
const value = rtl.cmdline.get_number(i64, cmdline, entry.name) catch |e| {
|
||||
if (e == error.Format) {
|
||||
std.log.warn("Invalid number value for '{s}', falling back.", .{entry.name});
|
||||
}
|
||||
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -87,15 +91,15 @@ pub fn init(boot_info: *pl.BootInfo) void {
|
||||
},
|
||||
|
||||
.bool => {
|
||||
const value = rtl.cmdline.get_string(cmdline, entries[i].name) orelse continue;
|
||||
const ptr: *bool = @ptrCast(@alignCast(entries[i].address));
|
||||
const value = rtl.cmdline.get_string(cmdline, entry.name) orelse continue;
|
||||
const ptr: *bool = @ptrCast(@alignCast(entry.address));
|
||||
|
||||
if (std.mem.eql(u8, value, "true")) {
|
||||
ptr.* = true;
|
||||
} else if (std.mem.eql(u8, value, "false")) {
|
||||
ptr.* = false;
|
||||
} else {
|
||||
std.log.warn("Invalid cmdline boolean value for tunable '{s}'", .{entries[i].name});
|
||||
std.log.warn("Invalid cmdline boolean value for tunable '{s}'", .{entry.name});
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user