xref: /cloud-hypervisor/hypervisor/src/arch/x86/mod.rs (revision d419e30df19475ae44b51a8668f64cad3135459e)
1 // Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 //
3 // Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE-BSD-3-Clause file.
6 //
7 // Copyright © 2019 Intel Corporation
8 //
9 // SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
10 //
11 // Copyright © 2020, Microsoft Corporation
12 //
13 
14 pub mod gdt;
15 
16 #[allow(non_upper_case_globals)]
17 #[allow(non_camel_case_types)]
18 #[allow(non_snake_case)]
19 #[allow(non_upper_case_globals)]
20 #[allow(unused)]
21 #[allow(
22     clippy::unreadable_literal,
23     clippy::redundant_static_lifetimes,
24     clippy::trivially_copy_pass_by_ref,
25     clippy::useless_transmute,
26     clippy::should_implement_trait,
27     clippy::transmute_ptr_to_ptr,
28     clippy::unreadable_literal,
29     clippy::redundant_static_lifetimes
30 )]
31 pub mod msr_index;
32 
33 pub mod emulator;
34 
35 // MTRR constants
36 pub const MTRR_ENABLE: u64 = 0x800; // IA32_MTRR_DEF_TYPE MSR: E (MTRRs enabled) flag, bit 11
37 pub const MTRR_MEM_TYPE_WB: u64 = 0x6;
38 
39 // IOAPIC pins
40 pub const NUM_IOAPIC_PINS: usize = 24;
41 
42 // X86 Exceptions
43 #[allow(dead_code)]
44 #[derive(Clone, Debug)]
45 pub enum Exception {
46     DE = 1,  // Divide Error
47     DB = 2,  // Debug Exception
48     BP = 3,  // Breakpoint
49     OF = 4,  // Overflow
50     BR = 5,  // BOUND Range Exceeded
51     UD = 6,  // Invalid/Undefined Opcode
52     NM = 7,  // No Math Coprocessor
53     DF = 8,  // Double Fault
54     TS = 10, // Invalid TSS
55     NP = 11, // Segment Not Present
56     SS = 12, // Stack Segment Fault
57     GP = 13, // General Protection
58     PF = 14, // Page Fault
59     MF = 16, // Math Fault
60     AC = 17, // Alignment Check
61     MC = 18, // Machine Check
62     XM = 19, // SIMD Floating-Point Exception
63     VE = 20, // Virtualization Exception
64     CP = 21, // Control Protection Exception
65 }
66 
67 pub mod regs;
68 
69 // Abstracted segment register ops.
70 // Each x86 hypervisor should implement those.
71 pub trait SegmentRegisterOps {
72     // Segment type
73     fn segment_type(&self) -> u8;
74     fn set_segment_type(&mut self, val: u8);
75 
76     // Descriptor Privilege Level (DPL)
77     fn dpl(&self) -> u8;
78     fn set_dpl(&mut self, val: u8);
79 
80     // Granularity
81     fn granularity(&self) -> u8;
82     fn set_granularity(&mut self, val: u8);
83 
84     // Memory Presence
85     fn present(&self) -> u8;
86     fn set_present(&mut self, val: u8);
87 
88     // Long mode
89     fn long(&self) -> u8;
90     fn set_long(&mut self, val: u8);
91 
92     // Available for system use (AVL)
93     fn avl(&self) -> u8;
94     fn set_avl(&mut self, val: u8);
95 
96     // Descriptor type (System or code/data)
97     fn desc_type(&self) -> u8;
98     fn set_desc_type(&mut self, val: u8);
99 
100     // D/B
101     fn db(&self) -> u8;
102     fn set_db(&mut self, val: u8);
103 }
104 
105 // Code segment
106 pub const CODE_SEGMENT_TYPE: u8 = 0x8;
107 
108 // Read/Write or Read/Exec segment
109 pub const RWRX_SEGMENT_TYPE: u8 = 0x2;
110 
111 // Expand down segment
112 pub const EXPAND_DOWN_SEGMENT_TYPE: u8 = 0x4;
113 
114 pub fn segment_type_code(t: u8) -> bool {
115     t & CODE_SEGMENT_TYPE != 0
116 }
117 
118 pub fn segment_type_ro(t: u8) -> bool {
119     t & !RWRX_SEGMENT_TYPE == 0
120 }
121 
122 pub fn segment_type_expand_down(t: u8) -> bool {
123     !segment_type_code(t) && (t & EXPAND_DOWN_SEGMENT_TYPE != 0)
124 }
125