Skip to content

Commit

Permalink
Refactor kcounter mod, fix wrong order when run /boot/bin/kcounter
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 committed Nov 1, 2021
1 parent 30831ed commit a6afbda
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 164 deletions.
101 changes: 0 additions & 101 deletions loader/src/kcounter.rs

This file was deleted.

2 changes: 0 additions & 2 deletions loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ cfg_if! {

cfg_if! {
if #[cfg(any(feature = "zircon", doc))] {
mod kcounter;

#[doc(cfg(feature = "zircon"))]
pub mod zircon;
}
Expand Down
48 changes: 44 additions & 4 deletions loader/src/zircon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Run Zircon user program (userboot) and manage trap/interrupt/syscall.
//!
//! Reference: <https://fuchsia.googlesource.com/fuchsia/+/3c234f79f71/zircon/kernel/lib/userabi/userboot.cc>
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use core::{future::Future, pin::Pin};
Expand All @@ -25,7 +27,7 @@ const K_ROOTRESOURCE: usize = 3;
const K_ZBI: usize = 4;
const K_FIRSTVDSO: usize = 5;
const K_CRASHLOG: usize = 8;
const K_COUNTERNAMES: usize = 9;
const K_COUNTER_NAMES: usize = 9;
const K_COUNTERS: usize = 10;
const K_FISTINSTRUMENTATIONDATA: usize = 11;
const K_HANDLECOUNT: usize = 15;
Expand Down Expand Up @@ -54,6 +56,39 @@ macro_rules! boot_library {
}};
}

fn kcounter_vmos() -> (Arc<VmObject>, Arc<VmObject>) {
let (desc_vmo, arena_vmo) = if cfg!(feature = "libos") {
// dummy VMOs
use zircon_object::util::kcounter::DescriptorVmoHeader;
const HEADER_SIZE: usize = core::mem::size_of::<DescriptorVmoHeader>();
let desc_vmo = VmObject::new_paged(1);
let arena_vmo = VmObject::new_paged(1);

let header = DescriptorVmoHeader::default();
let header_buf: [u8; HEADER_SIZE] = unsafe { core::mem::transmute(header) };
desc_vmo.write(0, &header_buf).unwrap();
(desc_vmo, arena_vmo)
} else {
use kernel_hal::vm::{GenericPageTable, PageTable};
use zircon_object::{util::kcounter::AllCounters, vm::pages};
let pgtable = PageTable::from_current();

// kcounters names table.
let desc_vmo_data = AllCounters::raw_desc_vmo_data();
let paddr = pgtable.query(desc_vmo_data.as_ptr() as usize).unwrap().0;
let desc_vmo = VmObject::new_physical(paddr, pages(desc_vmo_data.len()));

// kcounters live data.
let arena_vmo_data = AllCounters::raw_arena_vmo_data();
let paddr = pgtable.query(arena_vmo_data.as_ptr() as usize).unwrap().0;
let arena_vmo = VmObject::new_physical(paddr, pages(arena_vmo_data.len()));
(desc_vmo, arena_vmo)
};
desc_vmo.set_name("counters/desc");
arena_vmo.set_name("counters/arena");
(desc_vmo, arena_vmo)
}

/// Run Zircon `userboot` process from the prebuilt path, and load the ZBI file as the bootfs.
pub fn run_userboot(zbi: impl AsRef<[u8]>, cmdline: &str) -> Arc<Process> {
let userboot = boot_library!("userboot");
Expand Down Expand Up @@ -143,6 +178,7 @@ pub fn run_userboot(zbi: impl AsRef<[u8]>, cmdline: &str) -> Arc<Process> {
handles[K_ROOTJOB] = Handle::new(job, Rights::DEFAULT_JOB);
handles[K_ROOTRESOURCE] = Handle::new(resource, Rights::DEFAULT_RESOURCE);
handles[K_ZBI] = Handle::new(zbi_vmo, Rights::DEFAULT_VMO);

// set up handles[K_FIRSTVDSO..K_LASTVDSO + 1]
const VDSO_DATA_CONSTANTS: usize = 0x4a50;
const VDSO_DATA_CONSTANTS_SIZE: usize = 0x78;
Expand All @@ -157,13 +193,17 @@ pub fn run_userboot(zbi: impl AsRef<[u8]>, cmdline: &str) -> Arc<Process> {
handles[K_FIRSTVDSO] = Handle::new(vdso_vmo, Rights::DEFAULT_VMO | Rights::EXECUTE);
handles[K_FIRSTVDSO + 1] = Handle::new(vdso_test1, Rights::DEFAULT_VMO | Rights::EXECUTE);
handles[K_FIRSTVDSO + 2] = Handle::new(vdso_test2, Rights::DEFAULT_VMO | Rights::EXECUTE);

// TODO: use correct CrashLogVmo handle
let crash_log_vmo = VmObject::new_paged(1);
crash_log_vmo.set_name("crashlog");
handles[K_CRASHLOG] = Handle::new(crash_log_vmo, Rights::DEFAULT_VMO);
let (counter_name_vmo, kcounters_vmo) = super::kcounter::create_kcounter_vmo();
handles[K_COUNTERNAMES] = Handle::new(counter_name_vmo, Rights::DEFAULT_VMO);
handles[K_COUNTERS] = Handle::new(kcounters_vmo, Rights::DEFAULT_VMO);

// kcounter
let (desc_vmo, arena_vmo) = kcounter_vmos();
handles[K_COUNTER_NAMES] = Handle::new(desc_vmo, Rights::DEFAULT_VMO);
handles[K_COUNTERS] = Handle::new(arena_vmo, Rights::DEFAULT_VMO);

// TODO: use correct Instrumentation data handle
let instrumentation_data_vmo = VmObject::new_paged(0);
instrumentation_data_vmo.set_name("UNIMPLEMENTED_VMO");
Expand Down
36 changes: 22 additions & 14 deletions zCore/src/platform/x86/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ SECTIONS {

. = KERNEL_BEGIN;

.rodata ALIGN(4K):
{
*(.rodata .rodata.*)
}

.text ALIGN(4K):
{
stext = .;
Expand All @@ -21,12 +16,25 @@ SECTIONS {
etext = .;
}

.data ALIGN(4K):
.rodata ALIGN(4K):
{
PROVIDE_HIDDEN(kcounter_descriptor_begin = .);
KEEP(*(.kcounter.descriptor))
PROVIDE_HIDDEN(kcounter_descriptor_end = .);
*(.rodata .rodata.*)
}

.kcounter.desc ALIGN(4K):
{
PROVIDE_HIDDEN(kcounters_desc_vmo_start = .);
KEEP(*(.kcounter.desc.header))
QUAD(kcounters_desc_end - kcounters_desc_start);
ASSERT(. - kcounters_desc_vmo_start == 24, "wrong size of the kcounter descriptor VMO header");

PROVIDE_HIDDEN(kcounters_desc_start = .);
KEEP(*(SORT_BY_NAME(.kcounter.desc.*)))
PROVIDE_HIDDEN(kcounters_desc_end = .);
} :rodata

.data ALIGN(4K):
{
*(.data .data.*)
}

Expand All @@ -37,11 +45,11 @@ SECTIONS {

.bss ALIGN(4K):
{
. = ALIGN(4096);
PROVIDE_HIDDEN(kcounters_arena_start = .);
KEEP(*(.kcounter.items))
PROVIDE_HIDDEN(kcounters_arena_end = .);
. = ALIGN(4096);
PROVIDE_HIDDEN(kcounters_arena_start = .);
KEEP(*(SORT_BY_NAME(.bss.kcounter.*)))
PROVIDE_HIDDEN(kcounters_arena_end = .);
ASSERT(kcounters_arena_end - kcounters_arena_start == (kcounters_desc_end - kcounters_desc_start) * 8 / 64,
"kcounters_arena size mismatch");

*(.bss .bss.*)
}
Expand Down
Loading

0 comments on commit a6afbda

Please sign in to comment.