Skip to content

Commit

Permalink
svm: rename ebpf.zig to sbpf.zig
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexicon226 committed Jan 14, 2025
1 parent 0011adf commit 07eda86
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
16 changes: 8 additions & 8 deletions src/svm/elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Elf Spec: http://refspecs.linux-foundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic.html

const std = @import("std");
const ebpf = @import("ebpf.zig");
const sbpf = @import("sbpf.zig");
const memory = @import("memory.zig");

const lib = @import("lib.zig");
Expand All @@ -19,7 +19,7 @@ pub const Elf = struct {
headers: Headers,
data: Data,
entry_pc: u64,
version: ebpf.SBPFVersion,
version: sbpf.SBPFVersion,
function_registry: Registry(u32),

/// Contains immutable headers parsed from the ELF file.
Expand Down Expand Up @@ -209,7 +209,7 @@ pub const Elf = struct {
const offset = headers.header.e_entry -| text_section.sh_addr;
const entry_pc = try std.math.divExact(u64, offset, 8);

const sbpf_version: ebpf.SBPFVersion = if (headers.header.e_flags == ebpf.EF_SBPF_V2)
const sbpf_version: sbpf.SBPFVersion = if (headers.header.e_flags == sbpf.EF_SBPF_V2)
.v2
else
.v1;
Expand Down Expand Up @@ -312,11 +312,11 @@ pub const Elf = struct {
return error.WrongEndianess;
}
// ensure no OS_ABI was set
if (header.e_ident[ebpf.EI_OSABI] != ebpf.ELFOSABI_NONE) {
if (header.e_ident[sbpf.EI_OSABI] != sbpf.ELFOSABI_NONE) {
return error.WrongAbi;
}
// ensure the ELF was compiled for BPF or possibly the custom SBPF machine number
if (header.e_machine != elf.EM.BPF and @intFromEnum(header.e_machine) != ebpf.EM_SBPF) {
if (header.e_machine != elf.EM.BPF and @intFromEnum(header.e_machine) != sbpf.EM_SBPF) {
return error.WrongMachine;
}
// ensure that this is a `.so`, dynamic library file
Expand Down Expand Up @@ -521,7 +521,7 @@ pub const Elf = struct {
const slice = self.bytes[imm_offset..][0..4];
std.mem.writeInt(u32, slice, key, .little);
} else {
const hash = ebpf.hashSymbolName(symbol_name);
const hash = sbpf.hashSymbolName(symbol_name);
if (loader.functions.lookupKey(hash) == null) {
// return error.UnresolvedSymbol;
@panic(symbol_name);
Expand All @@ -535,11 +535,11 @@ pub const Elf = struct {
}
}

pub fn getInstructions(self: Elf) ![]align(1) const ebpf.Instruction {
pub fn getInstructions(self: Elf) ![]align(1) const sbpf.Instruction {
const text_section_index = self.getShdrIndexByName(".text") orelse
return error.ShdrNotFound;
const text_bytes: []const u8 = self.headers.shdrSlice(text_section_index);
return std.mem.bytesAsSlice(ebpf.Instruction, text_bytes);
return std.mem.bytesAsSlice(sbpf.Instruction, text_bytes);
}

fn getShdrIndexByName(self: Elf, name: []const u8) ?u32 {
Expand Down
38 changes: 19 additions & 19 deletions src/svm/executable.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const std = @import("std");
const ebpf = @import("ebpf.zig");
const sbpf = @import("sbpf.zig");
const Elf = @import("elf.zig").Elf;
const memory = @import("memory.zig");
const syscalls = @import("syscalls.zig");
const Vm = @import("vm.zig").Vm;

pub const Executable = struct {
bytes: []const u8,
instructions: []align(1) const ebpf.Instruction,
version: ebpf.SBPFVersion,
instructions: []align(1) const sbpf.Instruction,
version: sbpf.SBPFVersion,
entry_pc: u64,
from_elf: bool,
ro_section: Section,
Expand Down Expand Up @@ -59,12 +59,12 @@ pub const Executable = struct {
}

/// When the executable comes from the assembler, we need to guarantee that the
/// instructions are aligned to `ebpf.Instruction` rather than 1 like they would be
/// instructions are aligned to `sbpf.Instruction` rather than 1 like they would be
/// if we created the executable from the Elf file. The GPA requires allocations and
/// deallocations to be made with the same semantic alignment.
pub fn deinit(self: *Executable, allocator: std.mem.Allocator) void {
if (!self.from_elf) allocator.free(@as(
[]const ebpf.Instruction,
[]const sbpf.Instruction,
@alignCast(self.instructions),
));

Expand Down Expand Up @@ -95,13 +95,13 @@ pub const Assembler = struct {
};

const Operand = union(enum) {
register: ebpf.Instruction.Register,
register: sbpf.Instruction.Register,
integer: i64,
memory: Memory,
label: []const u8,

const Memory = struct {
base: ebpf.Instruction.Register,
base: sbpf.Instruction.Register,
offset: i16,
};
};
Expand Down Expand Up @@ -147,7 +147,7 @@ pub const Assembler = struct {
}
}

var instructions: std.ArrayListUnmanaged(ebpf.Instruction) = .{};
var instructions: std.ArrayListUnmanaged(sbpf.Instruction) = .{};
defer instructions.deinit(allocator);
inst_ptr = 0;

Expand All @@ -158,20 +158,20 @@ pub const Assembler = struct {
const name = inst.name;
const operands = inst.operands;

const bind = ebpf.Instruction.map.get(name) orelse
const bind = sbpf.Instruction.map.get(name) orelse
std.debug.panic("invalid instruction: {s}", .{name});

const instruction: ebpf.Instruction = switch (bind.inst) {
const instruction: sbpf.Instruction = switch (bind.inst) {
.alu_binary => inst: {
const is_immediate = operands[1] == .integer;
break :inst if (is_immediate) .{
.opcode = @enumFromInt(bind.opc | ebpf.Instruction.k),
.opcode = @enumFromInt(bind.opc | sbpf.Instruction.k),
.dst = operands[0].register,
.src = .r0,
.off = 0,
.imm = @bitCast(@as(i32, @intCast(operands[1].integer))),
} else .{
.opcode = @enumFromInt(bind.opc | ebpf.Instruction.x),
.opcode = @enumFromInt(bind.opc | sbpf.Instruction.x),
.dst = operands[0].register,
.src = operands[1].register,
.off = 0,
Expand Down Expand Up @@ -200,13 +200,13 @@ pub const Assembler = struct {
@panic("TODO: label jump");
} else {
break :inst if (is_immediate) .{
.opcode = @enumFromInt(bind.opc | ebpf.Instruction.k),
.opcode = @enumFromInt(bind.opc | sbpf.Instruction.k),
.dst = operands[0].register,
.src = .r0,
.off = @intCast(operands[2].integer),
.imm = @bitCast(@as(i32, @intCast(operands[1].integer))),
} else .{
.opcode = @enumFromInt(bind.opc | ebpf.Instruction.x),
.opcode = @enumFromInt(bind.opc | sbpf.Instruction.x),
.dst = operands[0].register,
.src = operands[1].register,
.off = @intCast(operands[2].integer),
Expand Down Expand Up @@ -370,7 +370,7 @@ pub const Assembler = struct {

while (iter.next()) |op| {
if (std.mem.startsWith(u8, op, "r")) {
const reg = std.meta.stringToEnum(ebpf.Instruction.Register, op) orelse
const reg = std.meta.stringToEnum(sbpf.Instruction.Register, op) orelse
@panic("unknown register");
try operands.append(allocator, .{ .register = reg });
} else if (std.mem.startsWith(u8, op, "[")) {
Expand All @@ -391,7 +391,7 @@ pub const Assembler = struct {
}

// otherwise it's just an address register argument
const reg = std.meta.stringToEnum(ebpf.Instruction.Register, base) orelse
const reg = std.meta.stringToEnum(sbpf.Instruction.Register, base) orelse
@panic("unknown register");

try operands.append(allocator, .{ .memory = .{
Expand Down Expand Up @@ -451,7 +451,7 @@ pub fn Registry(T: type) type {
name: []const u8,
value: T,
) !u32 {
const key = ebpf.hashSymbolName(name);
const key = sbpf.hashSymbolName(name);
try registry.register(allocator, key, name, value);
return key;
}
Expand All @@ -463,9 +463,9 @@ pub fn Registry(T: type) type {
value: T,
) !u32 {
const hash = if (std.mem.eql(u8, name, "entrypoint"))
ebpf.hashSymbolName(name)
sbpf.hashSymbolName(name)
else
ebpf.hashSymbolName(&std.mem.toBytes(value));
sbpf.hashSymbolName(&std.mem.toBytes(value));
try registry.register(allocator, hash, &.{}, value);
return hash;
}
Expand Down
2 changes: 1 addition & 1 deletion src/svm/lib.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const executable = @import("executable.zig");
pub const ebpf = @import("ebpf.zig");
pub const sbpf = @import("sbpf.zig");
pub const elf = @import("elf.zig");
pub const memory = @import("memory.zig");
pub const syscalls = @import("syscalls.zig");
Expand Down
4 changes: 2 additions & 2 deletions src/svm/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Elf = svm.Elf;
const memory = svm.memory;
const Executable = svm.Executable;
const Vm = svm.Vm;
const ebpf = svm.ebpf;
const sbpf = svm.sbpf;
const syscalls = svm.syscalls;

const MemoryMap = memory.MemoryMap;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn main() !void {
const input_file = try std.fs.cwd().openFile(input_path.?, .{});
defer input_file.close();

const bytes = try input_file.readToEndAlloc(allocator, ebpf.MAX_FILE_SIZE);
const bytes = try input_file.readToEndAlloc(allocator, sbpf.MAX_FILE_SIZE);
defer allocator.free(bytes);

var loader: svm.BuiltinProgram = .{};
Expand Down
8 changes: 4 additions & 4 deletions src/svm/memory.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const ebpf = @import("ebpf.zig");
const sbpf = @import("sbpf.zig");

pub const PROGRAM_START: u64 = 0x100000000;
pub const STACK_START: u64 = 0x200000000;
Expand All @@ -11,7 +11,7 @@ pub const MemoryMap = union(enum) {
aligned: AlignedMemoryMap,
// TODO: unaligned memory map?

pub fn init(regions: []const Region, version: ebpf.SBPFVersion) !MemoryMap {
pub fn init(regions: []const Region, version: sbpf.SBPFVersion) !MemoryMap {
return .{ .aligned = try AlignedMemoryMap.init(regions, version) };
}

Expand Down Expand Up @@ -113,9 +113,9 @@ pub const Region = struct {

const AlignedMemoryMap = struct {
regions: []const Region,
version: ebpf.SBPFVersion,
version: sbpf.SBPFVersion,

fn init(regions: []const Region, version: ebpf.SBPFVersion) !AlignedMemoryMap {
fn init(regions: []const Region, version: sbpf.SBPFVersion) !AlignedMemoryMap {
for (regions, 1..) |reg, index| {
if (reg.vm_addr_start >> VIRTUAL_ADDRESS_BITS != index) {
return error.InvalidMemoryRegion;
Expand Down
2 changes: 1 addition & 1 deletion src/svm/ebpf.zig → src/svm/sbpf.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Constants to do with Solana's EBPF
//! Constants to do with Solana's sbpf
const std = @import("std");
const memory = @import("memory.zig");
const assert = std.debug.assert;
Expand Down
4 changes: 2 additions & 2 deletions src/svm/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const lib = @import("lib.zig");
const memory = @import("memory.zig");
const Vm = @import("vm.zig").Vm;
const syscalls = @import("syscalls.zig");
const ebpf = @import("ebpf.zig");
const sbpf = @import("sbpf.zig");
const Elf = @import("elf.zig").Elf;

const Executable = lib.Executable;
Expand Down Expand Up @@ -1590,7 +1590,7 @@ fn testElfWithSyscalls(
const allocator = std.testing.allocator;

const input_file = try std.fs.cwd().openFile(path, .{});
const bytes = try input_file.readToEndAlloc(allocator, ebpf.MAX_FILE_SIZE);
const bytes = try input_file.readToEndAlloc(allocator, sbpf.MAX_FILE_SIZE);
defer allocator.free(bytes);

var loader: BuiltinProgram = .{};
Expand Down
8 changes: 4 additions & 4 deletions src/svm/vm.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const std = @import("std");
const lib = @import("lib.zig");
const ebpf = @import("ebpf.zig");
const sbpf = @import("sbpf.zig");
const memory = @import("memory.zig");

const MemoryMap = memory.MemoryMap;
const Instruction = ebpf.Instruction;
const Instruction = sbpf.Instruction;
const Executable = lib.Executable;
const BuiltinProgram = lib.BuiltinProgram;

Expand All @@ -14,7 +14,7 @@ pub const Vm = struct {
allocator: std.mem.Allocator,
executable: *const Executable,

registers: std.EnumArray(ebpf.Instruction.Register, u64),
registers: std.EnumArray(sbpf.Instruction.Register, u64),
memory_map: MemoryMap,
loader: *const BuiltinProgram,

Expand All @@ -39,7 +39,7 @@ pub const Vm = struct {
var self: Vm = .{
.executable = executable,
.allocator = allocator,
.registers = std.EnumArray(ebpf.Instruction.Register, u64).initFill(0),
.registers = std.EnumArray(sbpf.Instruction.Register, u64).initFill(0),
.memory_map = memory_map,
.stack_pointer = memory.STACK_START + 4096,
.depth = 0,
Expand Down

0 comments on commit 07eda86

Please sign in to comment.