Create a new memory chunk num_pages - Size of the memory chunk max_physaddr - If Some() then consecutive frames are allocated which are all below the maximum physical address. Returns either (handle, physaddr) or error code
(
num_pages: u64,
max_physaddr: u64
)
| 778 | /// |
| 779 | /// Returns either (handle, physaddr) or error code |
| 780 | pub fn new_memory_chunk( |
| 781 | num_pages: u64, |
| 782 | max_physaddr: u64 |
| 783 | ) -> Result<(VirtAddr, PhysAddr), usize> { |
| 784 | // Get the current thread |
| 785 | if let Some(thread) = CURRENT_THREAD.read().as_ref() { |
| 786 | |
| 787 | // Virtual address of the available page chunk |
| 788 | let start_addr = match memory::find_available_page_chunk( |
| 789 | thread.page_table_physaddr) { |
| 790 | Some(values) => values, |
| 791 | None => { |
| 792 | println!("Thread {} no available chunks!", thread.tid()); |
| 793 | return Err(syscalls::SYSCALL_ERROR_MEMORY) |
| 794 | } |
| 795 | }; |
| 796 | |
| 797 | if max_physaddr != 0 { |
| 798 | // Allocate a consecutive set of frames |
| 799 | let physaddr = match memory::create_consecutive_pages( |
| 800 | thread.page_table_physaddr, |
| 801 | start_addr, |
| 802 | num_pages, |
| 803 | max_physaddr) { |
| 804 | Ok(physaddr) => physaddr, |
| 805 | Err(_) => return Err(syscalls::SYSCALL_ERROR_MEMORY) |
| 806 | }; |
| 807 | |
| 808 | return Ok((start_addr, physaddr)); |
| 809 | } else { |
| 810 | // User doesn't need frames to be consecutive |
| 811 | // -> Allocate frames only when actually used |
| 812 | if memory::create_user_ondemand_pages( |
| 813 | thread.page_table_physaddr, |
| 814 | start_addr, |
| 815 | num_pages * 4096).is_err() { // size in bytes |
| 816 | return Err(syscalls::SYSCALL_ERROR_MEMORY); |
| 817 | } |
| 818 | |
| 819 | // Note: physical address not returned because |
| 820 | // the frames are not guaranteed to be |
| 821 | // consecutive in physical address. |
| 822 | return Ok((start_addr, PhysAddr::new(0))); |
| 823 | } |
| 824 | } |
| 825 | Err(syscalls::SYSCALL_ERROR_THREAD) |
| 826 | } |
| 827 | |
| 828 | /// A memory chunk which maps a specific range of |
| 829 | /// physical memory |
no test coverage detected