either_copyin and either_copyout

This commit is contained in:
Garen Tyler 2023-11-12 13:47:22 -07:00
parent c39b98dd02
commit 50b6016144
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
4 changed files with 47 additions and 41 deletions

View File

@ -8,7 +8,6 @@
struct proc proc[NPROC];
struct proc *initproc;
extern void forkret(void);
// helps ensure that wakeups of wait()ing
// parents are not lost. helps obey the
@ -76,33 +75,3 @@ forkret(void)
usertrapret();
}
// Copy to either a user address, or kernel address,
// depending on usr_dst.
// Returns 0 on success, -1 on error.
int
either_copyout(int user_dst, uint64 dst, void *src, uint64 len)
{
struct proc *p = myproc();
if(user_dst){
return copyout(p->pagetable, dst, src, len);
} else {
memmove((char *)dst, src, len);
return 0;
}
}
// Copy from either a user address, or kernel address,
// depending on usr_src.
// Returns 0 on success, -1 on error.
int
either_copyin(void *dst, int user_src, uint64 src, uint64 len)
{
struct proc *p = myproc();
if(user_src){
return copyin(p->pagetable, dst, src, len);
} else {
memmove(dst, (char*)src, len);
return 0;
}
}

View File

@ -46,8 +46,9 @@ pub mod mem {
pub mod virtual_memory {
#[cfg(target_arch = "riscv64")]
pub use super::riscv::virtual_memory::{
copyin, copyinstr, copyout, kvminit as init, kvminithart as inithart, mappages, uvmalloc,
uvmcopy, uvmcreate, uvmdealloc, uvmfree, uvmunmap,
copyin, copyinstr, copyout, either_copyin, either_copyout, kvminit as init,
kvminithart as inithart, mappages, uvmalloc, uvmcopy, uvmcreate, uvmdealloc, uvmfree,
uvmunmap,
};
}

View File

@ -1,6 +1,6 @@
use super::{
asm,
mem::{make_satp, pte2pa, kstack},
mem::{kstack, make_satp, pte2pa},
plic::PLIC,
power::QEMU_POWER,
};
@ -17,6 +17,7 @@ use crate::{
kalloc::{kalloc, kfree},
memmove, memset,
},
proc::process::Process,
};
use core::ptr::{addr_of, addr_of_mut, null_mut};
@ -25,6 +26,9 @@ extern "C" {
pub static etext: [u8; 0];
/// trampoline.S
pub static trampoline: [u8; 0];
// pub fn either_copyin(dst: *mut u8, user_src: i32, src: u64, len: u64) -> i32;
// pub fn either_copyout(user_dst: i32, dst: u64, src: *mut u8, len: u64) -> i32;
}
/// The kernel's pagetable.
@ -113,7 +117,13 @@ pub unsafe fn kvmmake() -> Pagetable {
panic!("kalloc");
}
let virtual_addr = kstack(i) as u64;
kvmmap(pagetable, virtual_addr, page as u64, PAGE_SIZE as u64, PTE_R | PTE_W);
kvmmap(
pagetable,
virtual_addr,
page as u64,
PAGE_SIZE as u64,
PTE_R | PTE_W,
);
}
pagetable
@ -559,6 +569,36 @@ pub unsafe extern "C" fn copyin(
0
}
// Copy to either a user address, or kernel address,
// depending on usr_dst.
// Returns 0 on success, -1 on error.
#[no_mangle]
pub unsafe extern "C" fn either_copyout(user_dst: i32, dst: u64, src: *mut u8, len: u64) -> i32 {
let p = Process::current().unwrap();
if user_dst > 0 {
copyout(p.pagetable, dst, src, len)
} else {
memmove(dst as *mut u8, src, len as u32);
0
}
}
// Copy from either a user address, or kernel address,
// depending on usr_src.
// Returns 0 on success, -1 on error.
#[no_mangle]
pub unsafe extern "C" fn either_copyin(dst: *mut u8, user_src: i32, src: u64, len: u64) -> i32 {
let p = Process::current().unwrap();
if user_src > 0 {
copyin(p.pagetable, dst, src, len)
} else {
memmove(dst, src as *mut u8, len as u32);
0
}
}
/// Copy a null-terminated string from user to kernel.
///
/// Copy bytes to `dst` from virtual address `src_virtual_addr`

View File

@ -11,6 +11,7 @@
pub mod printf;
use crate::{
arch::virtual_memory::{either_copyin, either_copyout},
fs::file::{devsw, CONSOLE},
hardware::uart::Uart,
proc::{
@ -19,12 +20,7 @@ use crate::{
},
sync::mutex::Mutex,
};
use core::{ffi::c_void, ptr::addr_of_mut};
extern "C" {
fn either_copyin(dst: *mut c_void, user_src: i32, src: u64, len: u64) -> i32;
fn either_copyout(user_dst: i32, dst: u64, src: *mut c_void, len: u64) -> i32;
}
use core::ptr::addr_of_mut;
pub static UART0: &Uart = &crate::hardware::UARTS[0].1;