libintrovirt v0.57.4
IntroVirt introspection library
Loading...
Searching...
No Matches
Segment.hh
Go to the documentation of this file.
1/*
2 * Copyright 2021 Assured Information Security, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
20
21#include <cstdint>
22
23namespace introvirt {
24namespace x86 {
25
26class SegmentSelector final {
27 public:
33 uint16_t index() const { return value_ >> 3; }
34
41 bool table_indicator() const { return value_ & 0x4; }
42
48 uint16_t rpl() const { return value_ & 0x3; }
49
55 uint16_t value() const { return value_; }
56
57 explicit SegmentSelector(uint16_t value) : value_(value) {}
58
59 public:
60 const uint16_t value_;
61};
62
66class Segment final {
67 public:
72 uint64_t base() const { return base_; }
78 uint32_t limit() const { return limit_; }
79
85 SegmentSelector selector() const { return selector_; }
86
87 bool present() const { return p_; }
88
89 uint8_t dpl() const { return dpl_; }
90
91 bool s() const { return s_; }
92
93 uint8_t type() const { return type_; }
94
95 bool granularity() const {
96 verify_s();
97 return g_;
98 }
99
100 bool db() const {
101 verify_s();
102 return db_;
103 }
104
105 bool long_mode() const {
106 verify_s();
107 return l_;
108 }
109
110 bool avl() const {
111 verify_s();
112 return avl_;
113 }
114
115 bool data() const {
116 verify_s();
117 return !code();
118 }
119
120 bool expand_down() const {
121 verify_data();
122 return type_ & 0x4;
123 }
124
125 bool writable() const {
126 verify_data();
127 return type_ & 0x2;
128 }
129
130 bool code() const {
131 verify_s();
132 return (type_ & 0x8);
133 }
134
135 bool conforming() const {
136 verify_code();
137 return type_ & 0x4;
138 }
139
140 bool readable() const {
141 verify_code();
142 return type_ & 0x2;
143 }
144
145 bool accessed() const {
146 verify_s();
147 return type_ & 0x1;
148 }
149
150 Segment(SegmentSelector sel, uint64_t base, uint32_t limit, uint8_t type, bool p, uint8_t dpl,
151 bool db, bool s, bool l, bool g, bool avl)
152 : selector_(sel), base_(base), limit_(limit), type_(type), p_(p), dpl_(dpl), db_(db), s_(s),
153 l_(l), g_(g), avl_(avl) {}
154
155 private:
156 void verify_s() const {
157 if (unlikely(s_ == 0))
159 }
160
161 void verify_code() const {
162 verify_s();
163 if (unlikely(!code()))
165 }
166
167 void verify_data() const {
168 verify_s();
169 if (unlikely(!data()))
170 throw InvalidMethodException();
171 }
172
173 private:
174 const SegmentSelector selector_;
175 const uint64_t base_;
176 const uint32_t limit_;
177 const uint8_t type_;
178 const bool p_;
179 const uint8_t dpl_;
180 const bool db_;
181 const bool s_;
182 const bool l_;
183 const bool g_;
184 const bool avl_;
185};
186
187} // namespace x86
188} // namespace introvirt
Thrown when the wrong method is called.
Definition InvalidMethodException.hh:29
Definition Segment.hh:26
SegmentSelector(uint16_t value)
Definition Segment.hh:57
uint16_t rpl() const
Get the requester privilege level.
Definition Segment.hh:48
const uint16_t value_
Definition Segment.hh:60
uint16_t index() const
Get the index into the descriptor table.
Definition Segment.hh:33
uint16_t value() const
Get the raw value of the selector.
Definition Segment.hh:55
bool table_indicator() const
Check if the selector is for the GDT or LDT.
Definition Segment.hh:41
Class to represent an x86 segment register.
Definition Segment.hh:66
uint8_t type() const
Definition Segment.hh:93
bool long_mode() const
Definition Segment.hh:105
bool readable() const
Definition Segment.hh:140
uint32_t limit() const
Get the size of the segment.
Definition Segment.hh:78
uint8_t dpl() const
Definition Segment.hh:89
bool present() const
Definition Segment.hh:87
bool granularity() const
Definition Segment.hh:95
bool accessed() const
Definition Segment.hh:145
bool s() const
Definition Segment.hh:91
bool data() const
Definition Segment.hh:115
bool expand_down() const
Definition Segment.hh:120
bool avl() const
Definition Segment.hh:110
SegmentSelector selector() const
Get the segment selector.
Definition Segment.hh:85
bool db() const
Definition Segment.hh:100
Segment(SegmentSelector sel, uint64_t base, uint32_t limit, uint8_t type, bool p, uint8_t dpl, bool db, bool s, bool l, bool g, bool avl)
Definition Segment.hh:150
bool conforming() const
Definition Segment.hh:135
uint64_t base() const
Get the base address of the segment.
Definition Segment.hh:72
bool writable() const
Definition Segment.hh:125
bool code() const
Definition Segment.hh:130
#define unlikely(x)
Definition compiler.hh:27
Core IntroVirt classes.
Definition Cr0.hh:20