Skip to content

Commit 87e8473

Browse files
authored
Merge branch 'main' into allocator-abstraction
2 parents 5dd7888 + c98578c commit 87e8473

File tree

8 files changed

+78
-176
lines changed

8 files changed

+78
-176
lines changed

src/arch/x86_64/kernel/apic.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,15 @@ fn detect_from_mp() -> Result<PhysAddr, ()> {
460460
(virtual_address | (u64::from(mp_float.mp_config) & (BasePageSize::SIZE - 1))) as usize;
461461
let mp_config: &ApicConfigTable = unsafe { &*(ptr::with_exposed_provenance(addr)) };
462462
if mp_config.signature != MP_CONFIG_SIGNATURE {
463-
warn!("Invalid MP config table");
463+
warn!("MP config table invalid!");
464464
unsafe {
465465
deallocate_virtual(virtual_address, BasePageSize::SIZE as usize);
466466
}
467467
return Err(());
468468
}
469469

470470
if mp_config.entry_count == 0 {
471-
warn!("No MP table entries! Guess IO-APIC!");
471+
warn!("No MP table entries, guessing IOAPIC...");
472472
let default_address = PhysAddr::new(0xfec0_0000);
473473

474474
init_ioapic_address(default_address);
@@ -490,7 +490,7 @@ fn detect_from_mp() -> Result<PhysAddr, ()> {
490490
2 => {
491491
let io_entry: &ApicIoEntry = unsafe { &*(ptr::with_exposed_provenance(addr)) };
492492
let ioapic = PhysAddr::new(io_entry.addr.into());
493-
info!("Found IOAPIC at 0x{ioapic:p}");
493+
info!("IOAPIC found at {ioapic:p}");
494494

495495
init_ioapic_address(ioapic);
496496

@@ -507,10 +507,10 @@ fn detect_from_mp() -> Result<PhysAddr, ()> {
507507
}
508508

509509
fn default_apic() -> PhysAddr {
510-
warn!("Try to use default APIC address");
511-
512510
let default_address = PhysAddr::new(0xfee0_0000);
513511

512+
warn!("Using default APIC address: {default_address:p}");
513+
514514
// currently, uhyve doesn't support an IO-APIC
515515
if !env::is_uhyve() {
516516
init_ioapic_address(default_address);
@@ -525,32 +525,30 @@ pub fn eoi() {
525525

526526
pub fn init() {
527527
// Detect CPUs and APICs.
528-
let local_apic_physical_address = detect_from_acpi()
529-
.or_else(|()| detect_from_mp())
530-
.unwrap_or_else(|()| default_apic());
528+
let local_apic_physical_address = if env::is_uhyve() {
529+
default_apic()
530+
} else {
531+
detect_from_acpi()
532+
.or_else(|()| detect_from_mp())
533+
.unwrap_or_else(|()| default_apic())
534+
};
531535

532536
// Initialize x2APIC or xAPIC, depending on what's available.
533-
init_x2apic();
534-
if !processor::supports_x2apic() {
537+
if processor::supports_x2apic() {
538+
init_x2apic();
539+
} else if env::is_uefi() {
540+
// already id mapped in UEFI systems, just use the physical address as virtual one
541+
LOCAL_APIC_ADDRESS
542+
.set(VirtAddr::new(local_apic_physical_address.as_u64()))
543+
.unwrap();
544+
} else {
535545
// We use the traditional xAPIC mode available on all x86-64 CPUs.
536546
// It uses a mapped page for communication.
537-
if env::is_uefi() {
538-
//already id mapped in UEFI systems, just use the physical address as virtual one
539-
LOCAL_APIC_ADDRESS
540-
.set(VirtAddr::new(local_apic_physical_address.as_u64()))
541-
.unwrap();
542-
} else {
543-
let local_apic_address =
547+
let local_apic_address =
544548
allocate_virtual(BasePageSize::SIZE as usize, BasePageSize::SIZE as usize).unwrap();
545-
LOCAL_APIC_ADDRESS.set(local_apic_address).unwrap();
546-
debug!(
547-
"Mapping Local APIC at {local_apic_physical_address:p} to virtual address {local_apic_address:p}"
548-
);
549-
550-
let mut flags = PageTableEntryFlags::empty();
551-
flags.device().writable().execute_disable();
552-
paging::map::<BasePageSize>(local_apic_address, local_apic_physical_address, 1, flags);
553-
}
549+
let mut flags = PageTableEntryFlags::empty();
550+
flags.device().writable().execute_disable();
551+
paging::map::<BasePageSize>(local_apic_address, local_apic_physical_address, 1, flags);
554552
}
555553

556554
// Set gates to ISRs for the APIC interrupts we are going to enable.
@@ -722,16 +720,14 @@ pub fn set_oneshot_timer(wakeup_time: Option<u64>) {
722720
}
723721

724722
pub fn init_x2apic() {
725-
if processor::supports_x2apic() {
726-
debug!("Enable x2APIC support");
727-
// The CPU supports the modern x2APIC mode, which uses MSRs for communication.
728-
// Enable it.
729-
let mut msr = IA32_APIC_BASE;
730-
let mut apic_base = unsafe { msr.read() };
731-
apic_base |= X2APIC_ENABLE;
732-
unsafe {
733-
msr.write(apic_base);
734-
}
723+
debug!("Enable x2APIC support");
724+
// The CPU supports the modern x2APIC mode, which uses MSRs for communication.
725+
// Enable it.
726+
let mut msr = IA32_APIC_BASE;
727+
let mut apic_base = unsafe { msr.read() };
728+
apic_base |= X2APIC_ENABLE;
729+
unsafe {
730+
msr.write(apic_base);
735731
}
736732
}
737733

src/arch/x86_64/kernel/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ pub fn application_processor_init() {
136136
processor::configure();
137137
gdt::add_current_core();
138138
interrupts::load_idt();
139-
apic::init_x2apic();
139+
if processor::supports_x2apic() {
140+
apic::init_x2apic();
141+
}
140142
apic::init_local_apic();
141143
debug!("Cr0 = {:?}", Cr0::read());
142144
debug!("Cr4 = {:?}", Cr4::read());

src/executor/vsock.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::collections::BTreeMap;
1+
use alloc::collections::{BTreeMap, btree_map};
22
use alloc::vec::Vec;
33
use core::future;
44
use core::task::Poll;
@@ -170,10 +170,15 @@ impl VsockMap {
170170
}
171171

172172
pub fn bind(&mut self, port: u32) -> io::Result<()> {
173-
self.port_map
174-
.try_insert(port, RawSocket::new(VsockState::Listen))
175-
.map_err(|_| Errno::Addrinuse)?;
176-
Ok(())
173+
let entry = self.port_map.entry(port);
174+
175+
match entry {
176+
btree_map::Entry::Vacant(vacant_entry) => {
177+
vacant_entry.insert(RawSocket::new(VsockState::Listen));
178+
Ok(())
179+
}
180+
btree_map::Entry::Occupied(_occupied_entry) => Err(Errno::Addrinuse),
181+
}
177182
}
178183

179184
pub fn connect(&mut self, port: u32, cid: u32) -> io::Result<u32> {
@@ -182,7 +187,8 @@ impl VsockMap {
182187
raw.remote_cid = cid;
183188
raw.remote_port = port;
184189

185-
if self.port_map.try_insert(i, raw).is_ok() {
190+
if let btree_map::Entry::Vacant(vacant_entry) = self.port_map.entry(i) {
191+
vacant_entry.insert(raw);
186192
return Ok(i);
187193
}
188194
}

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#![feature(allocator_api)]
1212
#![feature(linkage)]
1313
#![feature(linked_list_cursors)]
14-
#![feature(map_try_insert)]
1514
#![feature(maybe_uninit_as_bytes)]
1615
#![feature(maybe_uninit_slice)]
1716
#![feature(maybe_uninit_write_slice)]

src/mm/allocator.rs

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/mm/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
//! │ │ │ │
4141
//! ```
4242
43-
pub(crate) mod allocator;
4443
pub(crate) mod device_alloc;
4544
pub(crate) mod physicalmem;
4645
pub(crate) mod virtualmem;
@@ -49,10 +48,11 @@ use core::mem;
4948
use core::ops::Range;
5049

5150
use align_address::Align;
52-
use hermit_sync::Lazy;
51+
use free_list::{PageLayout, PageRange};
52+
use hermit_sync::{Lazy, RawInterruptTicketMutex};
5353
pub use memory_addresses::{PhysAddr, VirtAddr};
54+
use talc::{ErrOnOom, Span, Talc, Talck};
5455

55-
use self::allocator::LockedAllocator;
5656
#[cfg(any(target_arch = "x86_64", target_arch = "riscv64"))]
5757
use crate::arch::mm::paging::HugePageSize;
5858
pub use crate::arch::mm::paging::virtual_to_physical;
@@ -63,7 +63,7 @@ use crate::{arch, env};
6363

6464
#[cfg(target_os = "none")]
6565
#[global_allocator]
66-
pub(crate) static ALLOCATOR: LockedAllocator = LockedAllocator::new();
66+
pub(crate) static ALLOCATOR: Talck<RawInterruptTicketMutex, ErrOnOom> = Talc::new(ErrOnOom).lock();
6767

6868
/// Physical and virtual address range of the 2 MiB pages that map the kernel.
6969
static KERNEL_ADDR_RANGE: Lazy<Range<VirtAddr>> = Lazy::new(|| {
@@ -271,11 +271,9 @@ pub(crate) fn init() {
271271

272272
let heap_end_addr = map_addr;
273273

274+
let arena = Span::new(heap_start_addr.as_mut_ptr(), heap_end_addr.as_mut_ptr());
274275
unsafe {
275-
ALLOCATOR.init(
276-
heap_start_addr.as_mut_ptr(),
277-
(heap_end_addr - heap_start_addr) as usize,
278-
);
276+
ALLOCATOR.lock().claim(arena).unwrap();
279277
}
280278

281279
info!("Heap is located at {heap_start_addr:p}..{heap_end_addr:p} ({map_size} Bytes unmapped)");

src/scheduler/mod.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::sync::atomic::{AtomicI32, AtomicU32, Ordering};
1414

1515
use ahash::RandomState;
1616
use crossbeam_utils::Backoff;
17-
use hashbrown::HashMap;
17+
use hashbrown::{HashMap, hash_map};
1818
use hermit_sync::*;
1919
#[cfg(target_arch = "riscv64")]
2020
use riscv::register::sstatus;
@@ -564,10 +564,12 @@ impl PerCoreScheduler {
564564
};
565565

566566
let fd = new_fd()?;
567-
if object_map.try_insert(fd, obj).is_err() {
568-
Err(Errno::Mfile)
569-
} else {
570-
Ok(fd)
567+
match object_map.entry(fd) {
568+
hash_map::Entry::Occupied(_occupied_entry) => Err(Errno::Mfile),
569+
hash_map::Entry::Vacant(vacant_entry) => {
570+
vacant_entry.insert(obj);
571+
Ok(fd)
572+
}
571573
}
572574
})
573575
}
@@ -583,10 +585,12 @@ impl PerCoreScheduler {
583585

584586
let obj = object_map.get(&fd1).cloned().ok_or(Errno::Badf)?;
585587

586-
if object_map.try_insert(fd2, obj).is_err() {
587-
Err(Errno::Mfile)
588-
} else {
589-
Ok(fd2)
588+
match object_map.entry(fd2) {
589+
hash_map::Entry::Occupied(_occupied_entry) => Err(Errno::Mfile),
590+
hash_map::Entry::Vacant(vacant_entry) => {
591+
vacant_entry.insert(obj);
592+
Ok(fd2)
593+
}
590594
}
591595
})
592596
}

src/scheduler/task/mod.rs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -468,43 +468,19 @@ impl Task {
468468
let objmap = OBJECT_MAP.get().unwrap().clone();
469469
let mut guard = objmap.write();
470470
if env::is_uhyve() {
471-
guard
472-
.try_insert(
473-
STDIN_FILENO,
474-
Arc::new(async_lock::RwLock::new(UhyveStdin::new())),
475-
)
476-
.expect("cannot insert stdin");
477-
guard
478-
.try_insert(
479-
STDOUT_FILENO,
480-
Arc::new(async_lock::RwLock::new(UhyveStdout::new())),
481-
)
482-
.expect("cannot insert stdout");
483-
guard
484-
.try_insert(
485-
STDERR_FILENO,
486-
Arc::new(async_lock::RwLock::new(UhyveStderr::new())),
487-
)
488-
.expect("cannot insert stderr");
471+
let stdin = Arc::new(async_lock::RwLock::new(UhyveStdin::new()));
472+
let stdout = Arc::new(async_lock::RwLock::new(UhyveStdout::new()));
473+
let stderr = Arc::new(async_lock::RwLock::new(UhyveStderr::new()));
474+
guard.insert(STDIN_FILENO, stdin);
475+
guard.insert(STDOUT_FILENO, stdout);
476+
guard.insert(STDERR_FILENO, stderr);
489477
} else {
490-
guard
491-
.try_insert(
492-
STDIN_FILENO,
493-
Arc::new(async_lock::RwLock::new(GenericStdin::new())),
494-
)
495-
.expect("cannot insert stdin");
496-
guard
497-
.try_insert(
498-
STDOUT_FILENO,
499-
Arc::new(async_lock::RwLock::new(GenericStdout::new())),
500-
)
501-
.expect("cannot insert stdout");
502-
guard
503-
.try_insert(
504-
STDERR_FILENO,
505-
Arc::new(async_lock::RwLock::new(GenericStderr::new())),
506-
)
507-
.expect("cannot insert stderr");
478+
let stdin = Arc::new(async_lock::RwLock::new(GenericStdin::new()));
479+
let stdout = Arc::new(async_lock::RwLock::new(GenericStdout::new()));
480+
let stderr = Arc::new(async_lock::RwLock::new(GenericStderr::new()));
481+
guard.insert(STDIN_FILENO, stdin);
482+
guard.insert(STDOUT_FILENO, stdout);
483+
guard.insert(STDERR_FILENO, stderr);
508484
}
509485
}
510486

0 commit comments

Comments
 (0)