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
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#define _PAGE_BIT_PRESENT 0 /* is present */
|
|
|
|
#define _PAGE_BIT_RW 1 /* writeable */
|
|
|
|
#define _PAGE_BIT_USER 2 /* userspace addressable */
|
|
|
|
#define _PAGE_BIT_PWT 3 /* page write through */
|
|
|
|
#define _PAGE_BIT_PCD 4 /* page cache disabled */
|
|
|
|
#define _PAGE_BIT_ACCESSED 5 /* was accessed (raised by CPU) */
|
|
|
|
#define _PAGE_BIT_DIRTY 6 /* was written to (raised by CPU) */
|
|
|
|
#define _PAGE_BIT_PSE 7 /* 4 MB (or 2MB) page */
|
|
|
|
#define _PAGE_BIT_PAT 7 /* on 4KB pages */
|
|
|
|
#define _PAGE_BIT_GLOBAL 8 /* Global TLB entry PPro+ */
|
2011-08-23 07:40:20 -07:00
|
|
|
#define _PAGE_BIT_SVM_STRONG 9 /* mark a virtual address range as used by the SVM system */
|
|
|
|
#define _PAGE_BIT_SVM_LAZYRELEASE 10 /* mark a virtual address range as used by the SVM system */
|
2011-11-16 03:12:09 -08:00
|
|
|
#define _PAGE_BIT_SVM_INIT 11 /* mark if the MBP proxy is used */
|
2010-12-10 06:16:58 +00:00
|
|
|
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page is present
|
2010-12-10 06:16:58 +00:00
|
|
|
#define PG_PRESENT (1 << _PAGE_BIT_PRESENT)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page is read- and writable
|
2010-12-10 06:16:58 +00:00
|
|
|
#define PG_RW (1 << _PAGE_BIT_RW)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page is addressable from userspace
|
2010-12-10 06:16:58 +00:00
|
|
|
#define PG_USER (1 << _PAGE_BIT_USER)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page write through is activated
|
2010-12-10 06:16:58 +00:00
|
|
|
#define PG_PWT (1 << _PAGE_BIT_PWT)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page cache is disabled
|
2010-12-10 06:16:58 +00:00
|
|
|
#define PG_PCD (1 << _PAGE_BIT_PCD)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page was recently accessed (set by CPU)
|
2010-12-10 06:16:58 +00:00
|
|
|
#define PG_ACCESSED (1 << _PAGE_BIT_ACCESSED)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Page is dirty due to recentwrite-access (set by CPU)
|
2010-12-10 06:16:58 +00:00
|
|
|
#define PG_DIRTY (1 << _PAGE_BIT_DIRTY)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Big page: 4MB (or 2MB)
|
2010-12-10 06:16:58 +00:00
|
|
|
#define PG_PSE (1 << _PAGE_BIT_PSE)
|
2011-04-14 08:40:52 +02:00
|
|
|
/// Page is part of the MPB (SCC specific entry)
|
2011-04-05 23:43:44 -07:00
|
|
|
#define PG_MPE PG_PSE
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Global TLB entry (Pentium Pro and later)
|
2010-12-10 06:16:58 +00:00
|
|
|
#define PG_GLOBAL (1 << _PAGE_BIT_GLOBAL)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// Pattern flag
|
2010-12-10 06:16:58 +00:00
|
|
|
#define PG_PAT (1 << _PAGE_BIT_PAT)
|
2011-08-15 06:36:38 -07:00
|
|
|
/// This virtual address range is used by SVM system as marked
|
2011-11-08 09:22:49 -08:00
|
|
|
#define PG_SVM PG_SVM_STRONG
|
2011-08-23 07:40:20 -07:00
|
|
|
#define PG_SVM_STRONG (1 << _PAGE_BIT_SVM_STRONG)
|
|
|
|
/// This virtual address range is used by SVM system as marked
|
|
|
|
#define PG_SVM_LAZYRELEASE (1 << _PAGE_BIT_SVM_LAZYRELEASE)
|
2011-11-16 03:12:09 -08:00
|
|
|
/// Currently, no page frame is behind this page (only the MBP proxy)
|
|
|
|
#define PG_SVM_INIT (1 << _PAGE_BIT_SVM_INIT)
|
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
|
2010-12-10 06:16:58 +00:00
|
|
|
#define KERN_TABLE (PG_PRESENT|PG_RW|PG_ACCESSED|PG_DIRTY)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// This is a whole set of flags (PRESENT,RW,ACCESSED,DIRTY,USER) for userspace tables
|
2010-12-10 06:16:58 +00:00
|
|
|
#define USER_TABLE (PG_PRESENT|PG_RW|PG_ACCESSED|PG_DIRTY|PG_USER)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// This is a whole set of flags (PRESENT,RW,GLOBAL) for kernelspace pages
|
2010-12-10 06:16:58 +00:00
|
|
|
#define KERN_PAGE (PG_PRESENT|PG_RW|PG_GLOBAL)
|
2011-04-03 18:42:19 +02:00
|
|
|
/// This is a whole set of flags (PRESENT,RW,USER) for userspace pages
|
2010-12-10 06:16:58 +00:00
|
|
|
#define USER_PAGE (PG_PRESENT|PG_RW|PG_USER)
|
2012-05-24 10:49:45 +02:00
|
|
|
|
|
|
|
#if __SIZEOF_POINTER__ == 4
|
|
|
|
#define PGT_ENTRIES 1024
|
|
|
|
#elif __SIZEOF_POINTER__ == 8
|
|
|
|
#define PGT_ENTRIES 512
|
|
|
|
#endif
|
2010-12-10 06:16:58 +00:00
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Page table structure
|
|
|
|
*
|
|
|
|
* This structure keeps page table entries.\n
|
2012-06-10 08:05:24 +02:00
|
|
|
* On a 32bit system, a page table consists normally of 1024 entries.
|
2011-04-01 20:05:02 +02:00
|
|
|
*/
|
2010-12-10 06:16:58 +00:00
|
|
|
typedef struct page_table
|
|
|
|
{
|
2011-04-01 20:05:02 +02:00
|
|
|
/// Page table entries are unsigned 32bit integers.
|
2012-05-24 10:49:45 +02:00
|
|
|
size_t entries[PGT_ENTRIES];
|
2010-12-10 06:16:58 +00:00
|
|
|
} page_table_t __attribute__ ((aligned (4096)));
|
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Page directory structure
|
|
|
|
*
|
|
|
|
* This structure keeps page directory entries.\
|
2012-06-10 08:05:24 +02:00
|
|
|
* On a 32bit system, a page directory consists normally of 1024 entries.
|
2011-04-01 20:05:02 +02:00
|
|
|
*/
|
2010-12-10 06:16:58 +00:00
|
|
|
typedef struct page_dir
|
|
|
|
{
|
2011-04-01 20:05:02 +02:00
|
|
|
/// Page dir entries are unsigned 32bit integers.
|
2012-05-24 10:49:45 +02:00
|
|
|
size_t entries[PGT_ENTRIES];
|
2010-12-10 06:16:58 +00:00
|
|
|
} page_dir_t __attribute__ ((aligned (4096)));
|
|
|
|
|
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 Allocates a virtual address space range of npages
|
|
|
|
*
|
|
|
|
* The address range with special flags (if given) will have the size of n pages.
|
|
|
|
*
|
|
|
|
* @param npages The range in page-granularity
|
|
|
|
* @param flags further page flags
|
|
|
|
*
|
|
|
|
* @return The new range's address
|
2010-12-10 06:16:58 +00:00
|
|
|
*/
|
2011-02-24 10:15:58 +01:00
|
|
|
size_t vm_alloc(uint32_t npages, uint32_t flags);
|
2010-12-10 06:16:58 +00:00
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Frees a range in the virtual address space
|
|
|
|
*
|
|
|
|
* @param addr Address of the range
|
|
|
|
* @param npages Size of the range in pages
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - 0 on success
|
|
|
|
* - -EINVAL (-22) on failure.
|
2010-12-10 06:16:58 +00:00
|
|
|
*/
|
2011-02-24 10:15:58 +01:00
|
|
|
int vm_free(size_t addr, uint32_t npages);
|
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
|
|
|
*/
|
2011-03-04 11:38:40 +01:00
|
|
|
page_dir_t* get_boot_pgd(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
|
|
|
|
* @param copy If true: PGD will be a copy of the kernel's address space PGD
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - counter of allocated page tables
|
|
|
|
* - -ENOMEM (-12) on failure
|
2011-01-05 10:16:53 +00:00
|
|
|
*/
|
2011-03-02 13:49:36 +01:00
|
|
|
int create_pgd(task_t* task, int copy);
|
2011-01-05 10:16:53 +00:00
|
|
|
|
2011-04-01 20:05:02 +02:00
|
|
|
/** @brief Delete page directory and its page tables
|
|
|
|
*
|
|
|
|
* Puts page tables and page directory back to buffer and
|
|
|
|
* sets the task's page directory pointer to NULL
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - 0 on success
|
|
|
|
* - -EINVAL (-22) on failure (in case PGD is still the boot-pgd).
|
2011-02-24 18:32:58 +01:00
|
|
|
*/
|
|
|
|
int drop_pgd(void);
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
int change_page_permissions(size_t start, size_t end, uint32_t flags);
|
|
|
|
|
2010-12-10 06:16:58 +00:00
|
|
|
#endif
|