1/**
2 * \file
3 * \brief X86 inline asm utilities and defines
4 */
5
6/*
7 * Copyright (c) 2007, 2008, 2009, 2010, 2011, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#ifndef __X86_H
16#define __X86_H
17
18#include <barrelfish_kpi/types.h>
19#include <barrelfish_kpi/cpu.h>
20#include <arch/x86/x86.h>
21#include <barrelfish_kpi/cpu_arch.h>
22
23/***** EFLAGS flags *****/
24
25/**
26 * Allowed EFLAGS in user-space. Used when resuming programs.
27 */
28#define USER_EFLAGS_MASK \
29    (EFLAGS_CF | EFLAGS_PF | EFLAGS_AF | EFLAGS_ZF | EFLAGS_SF | EFLAGS_DF | \
30     EFLAGS_OF)
31
32
33#ifndef __ASSEMBLER__
34
35/**
36 * Registers automatically saved on kernel stack by CPU
37 */
38enum x86_32_cpu_save_registers {
39    X86_SAVE_EIP, X86_SAVE_CS, X86_SAVE_EFLAGS, X86_SAVE_ESP, X86_SAVE_SS,
40    X86_SAVE_AREA_SIZE
41};
42
43/** \brief Enable FPU */
44static inline void enable_fpu(void)
45{
46    uint32_t cr0;
47    uint32_t cr4;
48    __asm__ __volatile__("mov %%cr0, %%eax" : "=a" (cr0) : );
49    //clear EM
50    cr0 &= ~(1 << 2);
51    //set MP
52    cr0 |= (1 << 1);
53    //set NE
54    cr0 |= (1 << 5);
55    //clear TS
56    cr0 &= ~(1 << 3);
57    __asm__ __volatile__("mov %%eax,%%cr0" : : "a" (cr0));
58    //set OSFXSR
59    __asm__ __volatile__("mov %%cr4, %%eax" : "=a" (cr4) : );
60    cr4 |= (1 << 9);
61    __asm__ __volatile__("mov %%eax,%%cr4" : : "a" (cr4));
62    __asm volatile ("finit");
63}
64
65static inline void monitor(lvaddr_t base, uint32_t extensions, uint32_t hints)
66{
67    __asm volatile("monitor"
68                   : // No output
69                   :
70                   "a" (base),
71                   "c" (extensions),
72                   "d" (hints)
73                   );
74}
75
76static inline void mwait(uint32_t hints, uint32_t extensions)
77{
78    __asm volatile("mwait"
79                   : // No output
80                   :
81                   "a" (hints),
82                   "c" (extensions)
83                   );
84}
85
86#endif //__ASSEMBLER__
87
88#endif //__X86_H
89