Skip to content

Commit

Permalink
allocator: Fill allocated pages with zeroes by default (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuta committed Dec 22, 2021
1 parent 92e6bd6 commit 2852abd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
76 changes: 50 additions & 26 deletions kernel/arch/x64/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@ unsafe fn push_stack(mut rsp: *mut u64, value: u64) -> *mut u64 {
impl Process {
#[allow(unused)]
pub fn new_kthread(ip: VAddr, sp: VAddr) -> Process {
let interrupt_stack = alloc_pages(KERNEL_STACK_SIZE / PAGE_SIZE, AllocPageFlags::KERNEL)
.expect("failed to allocate kernel stack")
.as_vaddr();
let syscall_stack = alloc_pages(KERNEL_STACK_SIZE / PAGE_SIZE, AllocPageFlags::KERNEL)
.expect("failed to allocate kernel stack")
.as_vaddr();
let interrupt_stack = alloc_pages(
KERNEL_STACK_SIZE / PAGE_SIZE,
AllocPageFlags::KERNEL | AllocPageFlags::DIRTY_OK,
)
.expect("failed to allocate kernel stack")
.as_vaddr();
let syscall_stack = alloc_pages(
KERNEL_STACK_SIZE / PAGE_SIZE,
AllocPageFlags::KERNEL | AllocPageFlags::DIRTY_OK,
)
.expect("failed to allocate kernel stack")
.as_vaddr();

let rsp = unsafe {
let mut rsp: *mut u64 = sp.as_mut_ptr();
Expand Down Expand Up @@ -75,14 +81,20 @@ impl Process {
}

pub fn new_user_thread(ip: UserVAddr, sp: UserVAddr, kernel_sp: VAddr) -> Process {
let interrupt_stack = alloc_pages(KERNEL_STACK_SIZE / PAGE_SIZE, AllocPageFlags::KERNEL)
.expect("failed to allocate kernel stack")
.as_vaddr();
let syscall_stack = alloc_pages(KERNEL_STACK_SIZE / PAGE_SIZE, AllocPageFlags::KERNEL)
.expect("failed to allocate kernel stack")
.as_vaddr();
let interrupt_stack = alloc_pages(
KERNEL_STACK_SIZE / PAGE_SIZE,
AllocPageFlags::KERNEL | AllocPageFlags::DIRTY_OK,
)
.expect("failed to allocate kernel stack")
.as_vaddr();
let syscall_stack = alloc_pages(
KERNEL_STACK_SIZE / PAGE_SIZE,
AllocPageFlags::KERNEL | AllocPageFlags::DIRTY_OK,
)
.expect("failed to allocate kernel stack")
.as_vaddr();
// TODO: Check the size of XSAVE area.
let xsave_area = alloc_pages(1, AllocPageFlags::KERNEL | AllocPageFlags::ZEROED)
let xsave_area = alloc_pages(1, AllocPageFlags::KERNEL)
.expect("failed to allocate xsave area")
.as_vaddr();

Expand Down Expand Up @@ -119,12 +131,18 @@ impl Process {
}

pub fn new_idle_thread() -> Process {
let interrupt_stack = alloc_pages(KERNEL_STACK_SIZE / PAGE_SIZE, AllocPageFlags::KERNEL)
.expect("failed to allocate kernel stack")
.as_vaddr();
let syscall_stack = alloc_pages(KERNEL_STACK_SIZE / PAGE_SIZE, AllocPageFlags::KERNEL)
.expect("failed to allocate kernel stack")
.as_vaddr();
let interrupt_stack = alloc_pages(
KERNEL_STACK_SIZE / PAGE_SIZE,
AllocPageFlags::KERNEL | AllocPageFlags::DIRTY_OK,
)
.expect("failed to allocate kernel stack")
.as_vaddr();
let syscall_stack = alloc_pages(
KERNEL_STACK_SIZE / PAGE_SIZE,
AllocPageFlags::KERNEL | AllocPageFlags::DIRTY_OK,
)
.expect("failed to allocate kernel stack")
.as_vaddr();

Process {
rsp: UnsafeCell::new(0),
Expand All @@ -137,7 +155,7 @@ impl Process {

pub fn fork(&self, frame: &PtRegs) -> Result<Process> {
// TODO: Check the size of XSAVE area.
let xsave_area = alloc_pages(1, AllocPageFlags::KERNEL | AllocPageFlags::ZEROED)
let xsave_area = alloc_pages(1, AllocPageFlags::KERNEL)
.expect("failed to allocate xsave area")
.as_vaddr();

Expand Down Expand Up @@ -176,12 +194,18 @@ impl Process {
rsp
};

let interrupt_stack = alloc_pages(KERNEL_STACK_SIZE / PAGE_SIZE, AllocPageFlags::KERNEL)
.expect("failed allocate kernel stack")
.as_vaddr();
let syscall_stack = alloc_pages(KERNEL_STACK_SIZE / PAGE_SIZE, AllocPageFlags::KERNEL)
.expect("failed allocate kernel stack")
.as_vaddr();
let interrupt_stack = alloc_pages(
KERNEL_STACK_SIZE / PAGE_SIZE,
AllocPageFlags::KERNEL | AllocPageFlags::DIRTY_OK,
)
.expect("failed allocate kernel stack")
.as_vaddr();
let syscall_stack = alloc_pages(
KERNEL_STACK_SIZE / PAGE_SIZE,
AllocPageFlags::KERNEL | AllocPageFlags::DIRTY_OK,
)
.expect("failed allocate kernel stack")
.as_vaddr();

Ok(Process {
rsp: UnsafeCell::new(rsp as u64),
Expand Down
2 changes: 1 addition & 1 deletion libs/virtio/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl VirtQueue {

let virtqueue_paddr = alloc_pages(
align_up(virtq_size, PAGE_SIZE) / PAGE_SIZE,
AllocPageFlags::KERNEL | AllocPageFlags::ZEROED,
AllocPageFlags::KERNEL,
)
.expect("failed to allocate virtuqeue");

Expand Down
8 changes: 4 additions & 4 deletions runtime/page_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ bitflags! {
const KERNEL = 0;
/// Allocate pages for the user.
const USER = 0;
/// Fill allocated pages with zeroes.
const ZEROED = 1 << 0;
/// If it's not set, allocated pages will be filled with zeroes.
const DIRTY_OK = 1 << 0;
}
}

Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn alloc_pages(num_pages: usize, flags: AllocPageFlags) -> Result<PAddr, Pag
let mut zones = ZONES.lock();
for zone in zones.iter_mut() {
if let Some(paddr) = zone.alloc_pages(order).map(PAddr::new) {
if flags.contains(AllocPageFlags::ZEROED) {
if !flags.contains(AllocPageFlags::DIRTY_OK) {
unsafe {
paddr
.as_mut_ptr::<u8>()
Expand All @@ -116,7 +116,7 @@ pub fn alloc_pages_owned(
let mut zones = ZONES.lock();
for zone in zones.iter_mut() {
if let Some(paddr) = zone.alloc_pages(order).map(PAddr::new) {
if flags.contains(AllocPageFlags::ZEROED) {
if !flags.contains(AllocPageFlags::DIRTY_OK) {
unsafe {
paddr
.as_mut_ptr::<u8>()
Expand Down

0 comments on commit 2852abd

Please sign in to comment.