2010-07-31 15:53:30 +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-04 18:12:08 +02:00
|
|
|
/**
|
|
|
|
* @author Stefan Lankes
|
|
|
|
* @file arch/x86/include/asm/processor.h
|
|
|
|
* @brief CPU-specific functions
|
|
|
|
*
|
|
|
|
* This file contains structures and functions related to CPU-specific assembler commands.
|
|
|
|
*/
|
|
|
|
|
2010-07-31 15:53:30 +00:00
|
|
|
#ifndef __ARCH_PROCESSOR_H__
|
|
|
|
#define __ARCH_PROCESSOR_H__
|
|
|
|
|
2010-08-05 11:53:02 +00:00
|
|
|
#include <metalsvm/stddef.h>
|
2010-09-10 22:18:55 +00:00
|
|
|
#include <asm/gdt.h>
|
2010-10-25 16:58:31 +00:00
|
|
|
#include <asm/apic.h>
|
2010-08-05 11:53:02 +00:00
|
|
|
#ifdef CONFIG_PCI
|
|
|
|
#include <asm/pci.h>
|
|
|
|
#endif
|
|
|
|
|
2010-07-31 15:53:30 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2011-05-28 23:35:46 +02:00
|
|
|
// feature list 1
|
|
|
|
#define CPU_FEATURE_FPU (1 << 0)
|
2012-07-22 13:16:17 +02:00
|
|
|
#define CPU_FEATURE_MSR (1 << 5)
|
|
|
|
#define CPU_FEATURE_APIC (1 << 9)
|
2011-05-28 23:35:46 +02:00
|
|
|
#define CPU_FEATURE_MMX (1 << 23)
|
2011-08-03 19:37:05 +02:00
|
|
|
#define CPU_FEATURE_FXSR (1 << 24)
|
2011-05-28 23:35:46 +02:00
|
|
|
#define CPU_FEATURE_SSE (1 << 25)
|
2011-08-03 19:37:05 +02:00
|
|
|
#define CPU_FEATURE_SSE2 (1 << 26)
|
2011-05-28 23:35:46 +02:00
|
|
|
|
|
|
|
// feature list 2
|
2012-07-22 13:16:17 +02:00
|
|
|
#define CPU_FEATURE_X2APIC (1 << 21)
|
2011-05-28 23:35:46 +02:00
|
|
|
#define CPU_FEATURE_AVX (1 << 28)
|
2012-07-22 13:16:17 +02:00
|
|
|
#define CPU_FEATURE_HYPERVISOR (1 << 31)
|
2011-05-28 23:35:46 +02:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t feature1, feature2;
|
|
|
|
} cpu_info_t;
|
|
|
|
|
|
|
|
extern cpu_info_t cpu_info;
|
|
|
|
|
|
|
|
// determine the cpu features
|
|
|
|
int cpu_detection(void);
|
|
|
|
|
|
|
|
inline static uint32_t has_fpu(void)
|
|
|
|
{
|
|
|
|
return (cpu_info.feature1 & CPU_FEATURE_FPU);
|
|
|
|
}
|
|
|
|
|
2012-07-22 13:16:17 +02:00
|
|
|
inline static uint32_t has_msr(void)
|
|
|
|
{
|
|
|
|
return (cpu_info.feature1 & CPU_FEATURE_MSR);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static uint32_t has_apic(void)
|
|
|
|
{
|
|
|
|
return (cpu_info.feature1 & CPU_FEATURE_APIC);
|
|
|
|
}
|
|
|
|
|
2011-05-28 23:35:46 +02:00
|
|
|
inline static uint32_t has_fxsr(void)
|
|
|
|
{
|
|
|
|
return (cpu_info.feature1 & CPU_FEATURE_FXSR);
|
|
|
|
}
|
|
|
|
|
2012-07-17 22:33:29 +02:00
|
|
|
inline static uint32_t has_sse(void)
|
2011-05-28 23:35:46 +02:00
|
|
|
{
|
|
|
|
return (cpu_info.feature1 & CPU_FEATURE_SSE);
|
|
|
|
}
|
|
|
|
|
2012-07-17 22:33:29 +02:00
|
|
|
inline static uint32_t has_sse2(void)
|
|
|
|
{
|
|
|
|
return (cpu_info.feature1 & CPU_FEATURE_SSE2);
|
|
|
|
}
|
|
|
|
|
2012-07-22 13:16:17 +02:00
|
|
|
inline static uint32_t has_x2apic(void)
|
|
|
|
{
|
|
|
|
return (cpu_info.feature2 & CPU_FEATURE_X2APIC);
|
|
|
|
}
|
|
|
|
|
2011-05-28 23:35:46 +02:00
|
|
|
inline static uint32_t has_avx(void)
|
|
|
|
{
|
|
|
|
return (cpu_info.feature2 & CPU_FEATURE_AVX);
|
|
|
|
}
|
|
|
|
|
2012-07-22 13:16:17 +02:00
|
|
|
inline static uint32_t on_hypervisor(void)
|
|
|
|
{
|
|
|
|
return (cpu_info.feature2 & CPU_FEATURE_HYPERVISOR);
|
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Read out time stamp counter
|
|
|
|
*
|
|
|
|
* The rdtsc asm command puts a 64 bit time stamp value
|
|
|
|
* into EDX:EAX.
|
|
|
|
*
|
|
|
|
* @return The 64 bit time stamp value
|
|
|
|
*/
|
2010-09-11 11:29:30 +00:00
|
|
|
inline static uint64_t rdtsc(void)
|
2010-07-31 15:53:30 +00:00
|
|
|
{
|
|
|
|
uint64_t x;
|
|
|
|
asm volatile ("rdtsc" : "=A" (x));
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Flush cache
|
|
|
|
*
|
|
|
|
* The wbinvd asm instruction which stands for "Write back and invalidate"
|
|
|
|
* is used here
|
|
|
|
*/
|
2010-09-11 11:29:30 +00:00
|
|
|
inline static void flush_cache(void) {
|
2011-08-26 08:38:30 +02:00
|
|
|
asm volatile ("wbinvd" ::: "memory");
|
2010-09-11 11:29:30 +00:00
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Invalidate cache
|
|
|
|
*
|
|
|
|
* The invd asm instruction which invalidates cache without writing back
|
|
|
|
* is used here
|
|
|
|
*/
|
2011-08-16 03:29:54 -07:00
|
|
|
inline static void invalidate_cache(void) {
|
2011-08-26 08:38:30 +02:00
|
|
|
asm volatile ("invd" ::: "memory");
|
2010-09-11 11:29:30 +00:00
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Get return value from EAX
|
|
|
|
*
|
|
|
|
* If there is some return value in eax, this is the C-way to get it into a var
|
|
|
|
* if the function did not return it the normal way.
|
|
|
|
*
|
|
|
|
* @return The return value which wasn't returned as usual.
|
|
|
|
*/
|
2010-09-11 11:29:30 +00:00
|
|
|
inline static int get_return_value(void) {
|
|
|
|
int ret;
|
|
|
|
asm volatile ("movl %%eax, %0" : "=r"(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
2010-09-07 21:10:59 +00:00
|
|
|
|
|
|
|
/* Force strict CPU ordering */
|
2012-07-17 22:33:29 +02:00
|
|
|
typedef void (*func_memory_barrier)(void);
|
|
|
|
|
|
|
|
extern func_memory_barrier mb;
|
|
|
|
extern func_memory_barrier rmb;
|
|
|
|
extern func_memory_barrier wmb;
|
2010-09-07 21:10:59 +00:00
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Read out CPU ID
|
|
|
|
*
|
|
|
|
* The cpuid asm-instruction does fill some information into registers and
|
|
|
|
* this function fills those register values into the given uint32_t vars.\n
|
|
|
|
* \n
|
|
|
|
* Some people are used to flush the pipeline with this instruction;
|
|
|
|
* There is another function for doing this in MetalSVM called flush_pipeline().
|
|
|
|
* It basically does the same. Just use it if you only want to flush the pipeline
|
|
|
|
* as it will be more comfortable because it does not take any parameters.
|
|
|
|
*
|
|
|
|
* @param code Input parameter for the cpuid instruction. Take a look into the intel manual.
|
|
|
|
* @param a EAX value will be stores here
|
|
|
|
* @param b EBX value will be stores here
|
|
|
|
* @param c ECX value will be stores here
|
|
|
|
* @param d EDX value will be stores here
|
|
|
|
*/
|
2010-11-24 19:05:04 +00:00
|
|
|
inline static void cpuid(uint32_t code, uint32_t* a, uint32_t* b, uint32_t* c, uint32_t* d) {
|
2012-06-10 21:38:01 +02:00
|
|
|
asm volatile ("cpuid" : "=a"(*a), "=b"(*b), "=c"(*c), "=d"(*d) : "0"(code), "2"(*c));
|
2010-11-24 19:05:04 +00:00
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Read MSR
|
|
|
|
*
|
|
|
|
* The asm instruction rdmsr which stands for "Read from model specific register"
|
|
|
|
* is used here.
|
|
|
|
*
|
|
|
|
* @param msr The parameter which rdmsr assumes in ECX
|
|
|
|
* @return The value rdmsr put into EDX:EAX
|
|
|
|
*/
|
2010-10-25 16:58:31 +00:00
|
|
|
inline static uint64_t rdmsr(uint32_t msr) {
|
|
|
|
uint32_t low, high;
|
|
|
|
|
|
|
|
asm volatile ("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
|
|
|
|
|
|
|
|
return ((uint64_t)high << 32) | low;
|
|
|
|
}
|
|
|
|
|
2012-07-22 13:16:17 +02:00
|
|
|
/** @brief Write a value to a Machine-Specific Registers (MSR)
|
|
|
|
*
|
|
|
|
* The asm instruction wrmsr which stands for "Write to model specific register"
|
|
|
|
* is used here.
|
|
|
|
*
|
|
|
|
* @param msr The MSR identifier
|
|
|
|
* @param value Value, which will be store in the MSR
|
|
|
|
*/
|
|
|
|
inline static void wrmsr(uint32_t msr, uint64_t value)
|
|
|
|
{
|
|
|
|
uint32_t low = value & 0xFFFFFFFF;
|
|
|
|
uint32_t high = value >> 32;
|
|
|
|
|
|
|
|
asm volatile("wrmsr" :: "a"(low), "c"(msr), "d"(high));
|
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Read cr0 register
|
|
|
|
* @return cr0's value
|
|
|
|
*/
|
2012-05-24 10:49:45 +02:00
|
|
|
static inline size_t read_cr0(void) {
|
|
|
|
size_t val;
|
2010-12-10 06:16:58 +00:00
|
|
|
asm volatile("mov %%cr0, %0" : "=r"(val));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Write a value into cr0 register
|
|
|
|
* @param val The value you want to write into cr0
|
|
|
|
*/
|
2012-05-24 10:49:45 +02:00
|
|
|
static inline void write_cr0(size_t val) {
|
2010-12-10 06:16:58 +00:00
|
|
|
asm volatile("mov %0, %%cr0" : : "r"(val));
|
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Read cr2 register
|
|
|
|
* @return cr2's value
|
|
|
|
*/
|
2012-05-24 10:49:45 +02:00
|
|
|
static inline size_t read_cr2(void) {
|
|
|
|
size_t val;
|
2011-03-02 13:49:36 +01:00
|
|
|
asm volatile("mov %%cr2, %0" : "=r"(val));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Read cr3 register
|
|
|
|
* @return cr3's value
|
|
|
|
*/
|
2012-05-24 10:49:45 +02:00
|
|
|
static inline size_t read_cr3(void) {
|
|
|
|
size_t val;
|
2010-12-10 06:16:58 +00:00
|
|
|
asm volatile("mov %%cr3, %0" : "=r"(val));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Write a value into cr3 register
|
|
|
|
* @param val The value you want to write into cr3
|
|
|
|
*/
|
2012-05-24 10:49:45 +02:00
|
|
|
static inline void write_cr3(size_t val) {
|
2010-12-10 06:16:58 +00:00
|
|
|
asm volatile("mov %0, %%cr3" : : "r"(val));
|
|
|
|
}
|
|
|
|
|
2011-04-14 08:58:07 +02:00
|
|
|
/** @brief Read cr4 register
|
|
|
|
* @return cr4's value
|
2011-04-04 18:12:08 +02:00
|
|
|
*/
|
2012-05-24 10:49:45 +02:00
|
|
|
static inline size_t read_cr4(void) {
|
|
|
|
size_t val;
|
2011-04-05 23:43:44 -07:00
|
|
|
asm volatile("mov %%cr4, %0" : "=r"(val));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2011-04-14 08:58:07 +02:00
|
|
|
/** @brief Write a value into cr4 register
|
|
|
|
* @param val The value you want to write into cr4
|
|
|
|
*/
|
2012-05-24 10:49:45 +02:00
|
|
|
static inline void write_cr4(size_t val) {
|
2011-04-05 23:43:44 -07:00
|
|
|
asm volatile("mov %0, %%cr4" : : "r"(val));
|
|
|
|
}
|
|
|
|
|
2011-07-19 07:16:49 +02:00
|
|
|
int ipi_tlb_flush(void);
|
|
|
|
|
2011-04-14 08:58:07 +02:00
|
|
|
/** @brief Flush a specific page entry in TLB
|
|
|
|
* @param addr The (virtual) address of the page to flush
|
|
|
|
*/
|
2010-12-10 06:16:58 +00:00
|
|
|
static inline void tlb_flush_one_page(uint32_t addr)
|
|
|
|
{
|
|
|
|
asm volatile("invlpg (%0)" : : "r"(addr) : "memory");
|
2011-07-19 07:16:49 +02:00
|
|
|
#if MAX_CORES > 1
|
|
|
|
/*
|
|
|
|
* Currently, we didn't support user-level threads.
|
|
|
|
* => User-level applications run only on one
|
|
|
|
* and we didn't flush the TLB of the other cores
|
|
|
|
*/
|
|
|
|
if (addr <= KERNEL_SPACE)
|
|
|
|
ipi_tlb_flush();
|
|
|
|
#endif
|
2010-12-10 06:16:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Invalidate the whole TLB
|
|
|
|
*
|
|
|
|
* Just reads cr3 and writes the same value back into it.
|
|
|
|
*/
|
2010-12-10 06:16:58 +00:00
|
|
|
static inline void tlb_flush(void)
|
|
|
|
{
|
|
|
|
uint32_t val = read_cr3();
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
write_cr3(val);
|
2011-07-19 07:16:49 +02:00
|
|
|
#if MAX_CORES > 1
|
|
|
|
ipi_tlb_flush();
|
|
|
|
#endif
|
2010-12-10 06:16:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Read EFLAGS
|
|
|
|
*
|
|
|
|
* @return The EFLAGS value
|
|
|
|
*/
|
2011-03-02 13:49:36 +01:00
|
|
|
static inline uint32_t read_eflags(void)
|
|
|
|
{
|
|
|
|
uint32_t result;
|
2011-09-22 21:36:05 +02:00
|
|
|
asm volatile ("pushf; pop %0" : "=r"(result));
|
2011-03-02 13:49:36 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-01 13:31:41 -07:00
|
|
|
/** @brief search the first most significant bit
|
2011-08-17 13:51:19 +02:00
|
|
|
*
|
|
|
|
* @param i source operand
|
2011-09-01 13:31:41 -07:00
|
|
|
* @return
|
|
|
|
* - first bit, which is set in the source operand
|
|
|
|
* - invalid value, if not bit ist set
|
2011-08-17 13:51:19 +02:00
|
|
|
*/
|
2011-09-01 13:31:41 -07:00
|
|
|
static inline size_t msb(size_t i)
|
2011-08-17 13:51:19 +02:00
|
|
|
{
|
2011-09-01 13:31:41 -07:00
|
|
|
size_t ret;
|
2011-08-17 13:51:19 +02:00
|
|
|
|
|
|
|
if (!i)
|
2011-09-01 13:31:41 -07:00
|
|
|
return (sizeof(size_t)*8);
|
2011-08-26 08:38:30 +02:00
|
|
|
asm volatile ("bsr %1, %0" : "=r"(ret) : "r"(i) : "cc");
|
2011-08-17 13:51:19 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-01 13:31:41 -07:00
|
|
|
/** @brief search the least significant bit
|
|
|
|
*
|
|
|
|
* @param i source operand
|
|
|
|
* @return
|
|
|
|
* - first bit, which is set in the source operand
|
|
|
|
* - invalid value, if not bit ist set
|
|
|
|
*/
|
|
|
|
static inline size_t lsb(size_t i)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
return (sizeof(size_t)*8);
|
|
|
|
asm volatile ("bsf %1, %0" : "=r"(ret) : "r"(i) : "cc");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/// A one-instruction-do-nothing
|
2011-04-06 05:38:44 -07:00
|
|
|
#define NOP1 asm volatile ("nop")
|
2011-04-04 18:12:08 +02:00
|
|
|
/// Do nothing for 2 instructions
|
2011-04-06 05:38:44 -07:00
|
|
|
#define NOP2 asm volatile ("nop;nop")
|
2011-04-04 18:12:08 +02:00
|
|
|
/// Do nothing for 4 instructions
|
2011-04-06 05:38:44 -07:00
|
|
|
#define NOP4 asm volatile ("nop;nop;nop;nop")
|
2011-04-04 18:12:08 +02:00
|
|
|
/// Do nothing for 8 instructions
|
2011-04-06 05:38:44 -07:00
|
|
|
#define NOP8 asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop")
|
2012-07-03 16:46:54 +02:00
|
|
|
#ifndef CONFIG_TICKLESS
|
|
|
|
#define HALT asm volatile ("hlt")
|
|
|
|
#else
|
|
|
|
#define HALT asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop")
|
|
|
|
#endif
|
2011-04-04 18:12:08 +02:00
|
|
|
|
|
|
|
/** @brief Init several subsystems
|
|
|
|
*
|
|
|
|
* This function calls the initialization procedures for:
|
|
|
|
* - GDT
|
|
|
|
* - APIC
|
|
|
|
* - PCI [if configured]
|
|
|
|
*
|
|
|
|
* @return 0 in any case
|
|
|
|
*/
|
2010-08-05 11:53:02 +00:00
|
|
|
inline static int system_init(void)
|
|
|
|
{
|
|
|
|
gdt_install();
|
2012-07-22 13:16:17 +02:00
|
|
|
cpu_detection();
|
2011-01-05 10:16:53 +00:00
|
|
|
apic_init();
|
2010-08-05 11:53:02 +00:00
|
|
|
#ifdef CONFIG_PCI
|
|
|
|
pci_init();
|
|
|
|
#endif
|
2011-05-28 23:35:46 +02:00
|
|
|
|
2010-08-05 11:53:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-08-02 07:43:56 +00:00
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief Detect and read out CPU frequency
|
|
|
|
*
|
|
|
|
* @return The CPU frequency in MHz
|
|
|
|
*/
|
2010-12-10 06:16:58 +00:00
|
|
|
uint32_t detect_cpu_frequency(void);
|
2011-04-04 18:12:08 +02:00
|
|
|
|
|
|
|
/** @brief Read out CPU frequency if detected before
|
|
|
|
*
|
|
|
|
* If you did not issue the detect_cpu_frequency() function before,
|
|
|
|
* this function will call it implicitly.
|
|
|
|
*
|
|
|
|
* @return The CPU frequency in MHz
|
|
|
|
*/
|
2010-12-10 06:16:58 +00:00
|
|
|
uint32_t get_cpu_frequency(void);
|
2011-04-04 18:12:08 +02:00
|
|
|
|
|
|
|
/** @brief Busywait an microseconds interval of time
|
|
|
|
* @param usecs The time to wait in microseconds
|
|
|
|
*/
|
2010-12-10 06:16:58 +00:00
|
|
|
void udelay(uint32_t usecs);
|
|
|
|
|
2011-04-04 18:12:08 +02:00
|
|
|
/** @brief System calibration
|
|
|
|
*
|
|
|
|
* This procedure will detect the CPU frequency and calibrate the APIC timer.
|
|
|
|
*
|
|
|
|
* @return 0 in any case.
|
|
|
|
*/
|
2010-11-04 20:15:39 +00:00
|
|
|
inline static int system_calibration(void)
|
|
|
|
{
|
2010-12-10 06:16:58 +00:00
|
|
|
detect_cpu_frequency();
|
2010-11-04 20:15:39 +00:00
|
|
|
apic_calibration();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-31 15:53:30 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|