2010-12-10 06:16:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
|
|
|
|
* RWTH Aachen University
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* This file is part of MetalSVM.
|
|
|
|
*/
|
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/**
|
|
|
|
* @file arch/x86/include/asm/page.h
|
|
|
|
* @brief Definitions and functions related to paging
|
|
|
|
* @author Stefan Lankes
|
2013-12-03 15:26:21 +01:00
|
|
|
* @author Steffen Vogel <steffen.vogel@rwth-aachen.de>
|
2011-04-01 20:05:02 +02:00
|
|
|
*
|
|
|
|
* This file defines the interface for paging as like structures related to paging.
|
2010-12-10 06:16:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ARCH_PAGE_H__
|
|
|
|
#define __ARCH_PAGE_H__
|
|
|
|
|
|
|
|
#include <metalsvm/stddef.h>
|
|
|
|
#include <metalsvm/stdlib.h>
|
|
|
|
|
2013-12-03 15:26:21 +01:00
|
|
|
/// Page offset bits
|
2014-01-09 12:49:04 +01:00
|
|
|
#define PAGE_BITS 12
|
2014-02-18 13:08:22 +01:00
|
|
|
/// The size of a single page in bytes
|
|
|
|
#define PAGE_SIZE ( 1L << PAGE_BITS)
|
2013-10-24 12:36:05 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_X86_32
|
2013-12-03 16:34:34 +01:00
|
|
|
/// Total operand width in bits
|
|
|
|
#define BITS 32
|
2013-12-03 15:26:21 +01:00
|
|
|
/// Linear/virtual address width
|
2013-12-03 16:34:34 +01:00
|
|
|
#define VIRT_BITS BITS
|
2013-12-03 15:26:21 +01:00
|
|
|
/// Physical address width (we dont support PAE)
|
2013-12-03 16:34:34 +01:00
|
|
|
#define PHYS_BITS BITS
|
2013-12-03 15:26:21 +01:00
|
|
|
/// Page map bits
|
2014-02-18 13:08:22 +01:00
|
|
|
#define PAGE_MAP_BITS 10
|
|
|
|
/// Number of page map indirections
|
|
|
|
#define PAGE_MAP_LEVELS 2
|
|
|
|
/// Mask the page address without page map flags
|
|
|
|
#define PAGE_MASK 0xFFFFF000
|
|
|
|
#elif defined(CONFIG_X86_64)
|
2013-12-03 16:34:34 +01:00
|
|
|
/// Total operand width in bits
|
|
|
|
#define BITS 64
|
2013-12-03 15:26:21 +01:00
|
|
|
/// Linear/virtual address width
|
|
|
|
#define VIRT_BITS 48
|
|
|
|
/// Physical address width (maximum value)
|
|
|
|
#define PHYS_BITS 52
|
2014-02-18 13:08:22 +01:00
|
|
|
/// Page map bits
|
|
|
|
#define PAGE_MAP_BITS 9
|
|
|
|
/// Number of page map indirections
|
|
|
|
#define PAGE_MAP_LEVELS 4
|
|
|
|
/// Mask the page address without page map flags
|
|
|
|
#define PAGE_MASK 0x000FFFFFFFFFF000
|
2013-10-24 12:36:05 +02:00
|
|
|
#endif
|
|
|
|
|
2013-12-03 15:26:21 +01:00
|
|
|
/// The number of entries in a page map table
|
2014-02-18 13:08:22 +01:00
|
|
|
#define PAGE_MAP_ENTRIES (1L << PAGE_MAP_BITS)
|
2013-12-03 15:26:21 +01:00
|
|
|
|
2014-05-14 15:13:11 +02:00
|
|
|
// Base addresses of the self-mapped pagetables
|
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
#define PAGE_MAP_PGD 0xFFFFF000
|
|
|
|
#define PAGE_MAP_PGT 0xFFC00000
|
|
|
|
#elif defined(CONFIG_X86_64)
|
|
|
|
#define PAGE_MAP_PML4 0xFFFFFFFFFFFFF000
|
|
|
|
#define PAGE_MAP_PDPT 0xFFFFFFFFFFE00000
|
|
|
|
#define PAGE_MAP_PGD 0xFFFFFFFFC0000000
|
|
|
|
#define PAGE_MAP_PGT 0xFFFFFF8000000000
|
|
|
|
#endif
|
|
|
|
|
2013-12-03 15:29:05 +01:00
|
|
|
/// Align to next page
|
|
|
|
#define PAGE_FLOOR(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
|
|
|
|
/// Align to page
|
|
|
|
#define PAGE_CEIL(addr) ( (addr) & PAGE_MASK)
|
2013-10-24 12:36:05 +02:00
|
|
|
|
2014-05-14 15:13:11 +02:00
|
|
|
// Canonical address format
|
2014-05-14 18:56:15 +02:00
|
|
|
#ifdef CONFIG_X86_32
|
2014-05-14 15:13:11 +02:00
|
|
|
#define CANONICAL(addr) (addr)
|
|
|
|
#elif defined(CONFIG_X86_64)
|
|
|
|
#define CANONICAL(addr) sign_extend(addr, VIRT_BITS)
|
|
|
|
#endif
|
|
|
|
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page is present
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_PRESENT (1 << 0)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page is read- and writable
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_RW (1 << 1)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page is addressable from userspace
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_USER (1 << 2)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page write through is activated
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_PWT (1 << 3)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page cache is disabled
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_PCD (1 << 4)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page was recently accessed (set by CPU)
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_ACCESSED (1 << 5)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page is dirty due to recentwrite-access (set by CPU)
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_DIRTY (1 << 6)
|
2014-01-09 12:49:04 +01:00
|
|
|
/// Huge page: 4MB (or 2MB, 1GB)
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_PSE (1 << 7)
|
2011-04-14 08:40:52 +02:00
|
|
|
/// Page is part of the MPB (SCC specific entry)
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_MPE PG_PSE
|
2014-01-09 12:49:04 +01:00
|
|
|
/// Page attribute table
|
|
|
|
#define PG_PAT PG_PSE
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Global TLB entry (Pentium Pro and later)
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_GLOBAL (1 << 8)
|
2011-08-15 06:36:38 -07:00
|
|
|
/// This virtual address range is used by SVM system as marked
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_SVM (1 << 9)
|
|
|
|
#define PG_SVM_STRONG PG_SVM
|
2011-08-23 07:40:20 -07:00
|
|
|
/// This virtual address range is used by SVM system as marked
|
2013-10-24 12:36:05 +02:00
|
|
|
#define PG_SVM_LAZYRELEASE (1 << 10)
|
2011-11-16 03:12:09 -08:00
|
|
|
/// Currently, no page frame is behind this page (only the MBP proxy)
|
2013-12-03 15:26:21 +01:00
|
|
|
#define PG_SVM_INIT (1 << 11)
|
2014-01-09 12:49:04 +01:00
|
|
|
/// Disable execution for this page
|
|
|
|
#define PG_XD (1L << 63)
|
2010-12-10 06:16:58 +00:00
|
|
|
|
2011-04-03 18:42:19 +02:00
|
|
|
/// This is a whole set of flags (PRESENT,RW,ACCESSED,DIRTY) for kernelspace tables
|
2014-01-09 13:32:00 +01:00
|
|
|
#define PG_TABLE (PG_PRESENT|PG_RW|PG_XD)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// This is a whole set of flags (PRESENT,RW,GLOBAL) for kernelspace pages
|
2014-01-09 13:32:00 +01:00
|
|
|
#define PG_PAGE (PG_PRESENT|PG_RW|PG_GLOBAL|PG_XD)
|
2012-05-24 10:49:45 +02:00
|
|
|
|
2014-02-18 13:08:22 +01:00
|
|
|
/** @brief A single entry in a page map
|
2013-12-03 16:34:34 +01:00
|
|
|
*
|
2014-02-18 13:08:22 +01:00
|
|
|
* Usually used as a pointer to a mapped page map entry.
|
2013-12-03 16:34:34 +01:00
|
|
|
*/
|
2014-02-18 13:08:22 +01:00
|
|
|
typedef size_t page_entry_t;
|
2013-12-03 16:34:34 +01:00
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Converts a virtual address to a physical
|
|
|
|
*
|
|
|
|
* @param viraddr Virtual address to convert
|
|
|
|
* @return physical address
|
2010-12-10 06:16:58 +00:00
|
|
|
*/
|
2011-04-01 20:05:02 +02:00
|
|
|
size_t virt_to_phys(size_t viraddr);
|
2010-12-10 06:16:58 +00:00
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Unmap the physical memory at a specific virtual address
|
|
|
|
*
|
|
|
|
* All Page table entries within this range will be marked as not present
|
|
|
|
* and (in the case of userspace memory) the page usage of the task will be decremented.
|
|
|
|
*
|
|
|
|
* @param viraddr The range's virtual address
|
|
|
|
* @param npages The range's size in pages
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - 0 on success
|
|
|
|
* - -EINVAL (-22) on failure.
|
2011-03-04 22:44:53 +01:00
|
|
|
*/
|
|
|
|
int unmap_region(size_t viraddr, uint32_t npages);
|
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Mapping a physical mem-region to a virtual address
|
|
|
|
*
|
|
|
|
* Maps a physical memory region to a specific virtual address.
|
2010-12-10 06:16:58 +00:00
|
|
|
* If the virtual address is zero, this functions allocates a valid virtual address on demand.
|
2011-04-01 20:05:02 +02:00
|
|
|
*
|
|
|
|
* @param viraddr Desired virtual address
|
|
|
|
* @param phyaddr Physical address to map from
|
2011-08-15 06:36:38 -07:00
|
|
|
* @param npages The region's size in number of pages
|
2011-04-01 20:05:02 +02:00
|
|
|
* @param flags Further page flags
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - A virtual address on success
|
|
|
|
* - 0 on failure.
|
2010-12-10 06:16:58 +00:00
|
|
|
*/
|
2011-04-01 20:05:02 +02:00
|
|
|
size_t map_region(size_t viraddr, size_t phyaddr, uint32_t npages, uint32_t flags);
|
2010-12-10 06:16:58 +00:00
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Sets up the environment and enables paging.
|
|
|
|
*
|
|
|
|
* - Installs the page handler IRQ
|
|
|
|
* - sets up the whole page directory and page tables for the kernel space (virt adr = phys adr)
|
|
|
|
* - maps VGA, multi boot info and initrd into kernel space
|
|
|
|
* - writes to cr0 and cr3 register
|
|
|
|
* - marks 'paging_enabled' var = 1
|
|
|
|
* - Registers kernel thread for task state switching
|
|
|
|
*
|
|
|
|
* @return returns
|
|
|
|
* - 0 on success
|
|
|
|
* - -ENOMEM (-12) on failure
|
2010-12-10 06:16:58 +00:00
|
|
|
*/
|
|
|
|
int arch_paging_init(void);
|
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Returns the page directory of the boot task
|
|
|
|
*
|
|
|
|
* The boot task's page directory is a static array of page_dir_t type vars.
|
|
|
|
*
|
|
|
|
* @return Returns the address of the boot task's page dir array.
|
2010-12-10 06:16:58 +00:00
|
|
|
*/
|
2014-02-18 13:08:22 +01:00
|
|
|
page_entry_t* get_boot_page_map(void);
|
2010-12-10 06:16:58 +00:00
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Setup a new page directory for a new user-level task
|
|
|
|
*
|
|
|
|
* @param task Pointer to the task-specific task_t structure
|
2014-01-09 15:20:22 +01:00
|
|
|
* @param copy If true: copy userspace pages and tables
|
2011-04-01 20:05:02 +02:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - counter of allocated page tables
|
|
|
|
* - -ENOMEM (-12) on failure
|
2011-01-05 10:16:53 +00:00
|
|
|
*/
|
2014-01-09 15:20:22 +01:00
|
|
|
int copy_page_map(struct task* task, int copy);
|
2011-01-05 10:16:53 +00:00
|
|
|
|
2014-02-18 13:08:22 +01:00
|
|
|
/** @brief Deletes all user page map structures of the current task
|
2011-04-01 20:05:02 +02:00
|
|
|
*
|
2014-02-18 13:08:22 +01:00
|
|
|
* All allocated physical page frames are released in the bitmap
|
|
|
|
* The task->page_map is replaces by the boot_page_map()
|
2011-04-01 20:05:02 +02:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - 0 on success
|
|
|
|
* - -EINVAL (-22) on failure (in case PGD is still the boot-pgd).
|
2011-02-24 18:32:58 +01:00
|
|
|
*/
|
2013-10-11 16:21:53 +02:00
|
|
|
int drop_page_map(void);
|
2011-02-24 18:32:58 +01:00
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Change the page permission in the page tables of the current task
|
|
|
|
*
|
|
|
|
* Applies given flags noted in the 'flags' parameter to
|
|
|
|
* the range denoted by virtual start and end addresses.
|
|
|
|
*
|
|
|
|
* @param start Range's virtual start address
|
|
|
|
* @param end Range's virtual end address
|
|
|
|
* @param flags flags to apply
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - 0 on success
|
|
|
|
* - -EINVAL (-22) on failure.
|
2011-02-24 09:36:05 +01:00
|
|
|
*/
|
2014-02-18 13:08:22 +01:00
|
|
|
int set_page_flags(size_t viraddr, uint32_t npages, int flags);
|
2011-02-24 09:36:05 +01:00
|
|
|
|
2014-02-18 13:08:22 +01:00
|
|
|
/** @brief Dump mapped memory
|
|
|
|
*
|
|
|
|
* @param mask Only watch for changes in these page flags (PG_PRESENT is set by default)
|
|
|
|
*/
|
|
|
|
void page_dump(size_t mask);
|
2013-12-03 16:37:53 +01:00
|
|
|
|
|
|
|
/** @brief Print stats about page flags
|
|
|
|
*
|
|
|
|
* @param reset Reset accessed and dirty bits in page tables
|
|
|
|
*/
|
2014-02-18 13:08:22 +01:00
|
|
|
void page_stats(int reset);
|
2013-12-03 16:37:53 +01:00
|
|
|
|
2010-12-10 06:16:58 +00:00
|
|
|
#endif
|