genode/repos/base-hw/src/core/spec/x86_64/mode_transition.s

106 lines
2.5 KiB
ArmAsm
Raw Normal View History

/*
* \brief Transition between kernel/userland, and secure/non-secure world
* \author Martin Stein
* \author Stefan Kalkowski
* \date 2011-11-15
*/
/*
* Copyright (C) 2011-2013 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
.include "macros.s"
/* size of pointer to CPU context */
.set CONTEXT_PTR_SIZE, 1 * 8
/* globally mapped buffer storage */
.set BUFFER_SIZE, 6 * 8
/* offsets of the member variables in a CPU context */
.set SP_OFFSET, 1 * 8
.set R8_OFFSET, 2 * 8
.set RAX_OFFSET, 10 * 8
.set FLAGS_OFFSET, 18 * 8
.set CR3_OFFSET, 20 * 8
.section .text
/*
* Page aligned base of mode transition code.
*
* This position independent code switches between a kernel context and a
* user context and thereby between their address spaces. Due to the latter
* it must be mapped executable to the same region in every address space.
* To enable such switching, the kernel context must be stored within this
* region, thus one should map it solely accessable for privileged modes.
*/
.p2align MIN_PAGE_SIZE_LOG2
.global _mt_begin
_mt_begin:
/* space for a copy of the kernel context */
.p2align 2
.global _mt_master_context_begin
_mt_master_context_begin:
/* space must be at least as large as 'Cpu_state' */
hw_x86_64: Implementation of IA-32e paging IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses. Translation structures are hierarchical and four levels deep. The current implementation supports regular 4KB and 1 GB and 2 MB large page mappings. Memory typing is not yet implemented since the encoded type bits depend on the active page attribute table (PAT)*. For detailed information refer to Intel SDM Vol. 3A, section 4.5. * The default PAT after power up does not allow the encoding of the write-combining memory type, see Intel SDM Vol. 3A, section 11.12.4. * Add common IA-32e paging descriptor type: The type represents a table entry and encompasses all fields shared by paging structure entries of all four levels (PML4, PDPT, PD and PT). * Simplify PT entry type by using common descriptor: Differing fields are the physical address, the global flag and the memory type flags. * Simplify directory entry type by using common descriptor: Page directory entries (PDPT and PD) have an additional 'page size' field that specifies if the entry references a next level paging structure or represents a large page mapping. * Simplify PML4 entry type by using common descriptor Top-level paging structure entries (PML4) do not have a 'pat' flag and the memory type is specified by the 'pwt' and 'pcd' fields only. * Implement access right merging for directory paging entries The access rights for translations are determined by the U/S, R/W and XD flags. Paging structure entries that reference other tables must provide the superset of rights required for all entries of the referenced table. Thus merge access rights of new mappings into existing directory entries to grant additional rights if needed. * Add cr3 register definition: The control register 3 is used to set the current page-directory base register. * Add cr3 variable to x86_64 Cpu Context The variable designates the address of the top-level paging structure. * Return current cr3 value as translation table base * Set context cr3 value on translation table assignment * Implement switch to virtual mode in kernel Activate translation table in init_virt_kernel function by updating the cr3 register. * Ignore accessed and dirty flags when comparing existing table entries These flags can be set by the MMU and must be disregarded.
2015-02-16 15:20:06 +01:00
.space 21*8
.global _mt_master_context_end
_mt_master_context_end:
/* space for a client context-pointer per CPU */
.p2align 2
.global _mt_client_context_ptr
_mt_client_context_ptr:
.space CONTEXT_PTR_SIZE
/* a globally mapped buffer per CPU */
.p2align 2
.global _mt_buffer
_mt_buffer:
.space BUFFER_SIZE
/*
* On user exceptions the CPU has to jump to one of the following
* seven entry vectors to switch to a kernel context.
*/
.global _mt_kernel_entry_pic
_mt_kernel_entry_pic:
1: jmp 1b
.global _mt_user_entry_pic
_mt_user_entry_pic:
/* Prepare stack frame in mt buffer (Intel SDM Vol. 3A, figure 6-8) */
mov _mt_client_context_ptr, %rax
mov $_mt_buffer+BUFFER_SIZE, %rsp
pushq $0x23
pushq SP_OFFSET(%rax)
/* Set I/O privilege level to 3 */
orq $0x3000, FLAGS_OFFSET(%rax)
btrq $9, FLAGS_OFFSET(%rax) /* XXX: Drop once interrupt handling is done */
pushq FLAGS_OFFSET(%rax)
pushq $0x1b
pushq (%rax)
/* Restore segment registers */
mov $0x23, %rbx
mov %rbx, %ds
mov %rbx, %es
mov %rbx, %fs
mov %rbx, %gs
1: jmp 1b
/* end of the mode transition code */
.global _mt_end
_mt_end:
1: jmp 1b