/
Model.hs
446 lines (390 loc) · 11.2 KB
/
Model.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
--
-- Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
--
-- SPDX-License-Identifier: BSD-2-Clause
--
{-# LANGUAGE DeriveDataTypeable, TemplateHaskell #-}
module CapDL.Model where
import Prelude ()
import Prelude.Compat
import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import Data.Data
import Data.Word
import Control.Lens
-- Supported architectures:
data Arch = IA32 | ARM11 | X86_64 | AARCH64 | RISCV deriving (Eq, Show)
-- Access rights of capabilities. Not all capability types support all rights.
data Rights = Read
| Write
| Grant
| GrantReply
deriving (Eq, Show, Enum, Ord, Typeable, Data)
type CapRights = Set Rights
allRights :: CapRights
allRights = Set.fromList [Read, Write, Grant, GrantReply]
--
-- Object identifiers.
--
-- Currently, each object is described by a string (its name), possibly
-- followed by a word (an index). The latter feature allows arrays of
-- objects to be specified without having to give them explicit names
-- (for instance, when declaring large numbers of frames for a process).
--
-- These may later be converted to a type variable.
--
type ObjID = (String, Maybe Word)
type Asid = (Word, Word)
--
-- Capabilities.
--
-- Each cap has a type (what the cap does), and a bunch of attributes
-- (such as the rights, what object the capability gives access to,
-- etc).
--
data Cap
= NullCap
| UntypedCap { capObj :: ObjID }
| EndpointCap {
capObj :: ObjID,
capBadge :: Word,
capRights :: CapRights }
| NotificationCap {
capObj :: ObjID,
capBadge :: Word,
capRights :: CapRights }
| ReplyCap { capObj :: ObjID }
| MasterReplyCap { capObj :: ObjID }
| CNodeCap {
capObj :: ObjID,
capGuard :: Word,
capGuardSize :: Word }
| TCBCap { capObj :: ObjID }
| IRQControlCap
| IRQHandlerCap { capObj :: ObjID }
| IRQIOAPICHandlerCap {
capObj :: ObjID }
| IRQMSIHandlerCap {
capObj :: ObjID }
| DomainCap
| SCCap { capObj :: ObjID }
| SchedControlCap { core :: Word }
| RTReplyCap { capObj :: ObjID }
| VCPUCap {
capObj :: ObjID }
-- arch specific caps, ARM11, IA32, X86_64 and AARCH64 merged
| FrameCap {
capObj :: ObjID,
capRights :: CapRights,
capMaybeAsid :: Maybe Asid,
capCached :: Bool,
capMaybeMapping :: Maybe (ObjID, Word) }
| PTCap {
capObj :: ObjID,
capMaybeAsid :: Maybe Asid }
| PDCap {
capObj :: ObjID,
capMaybeAsid :: Maybe Asid }
| PDPTCap {
capObj :: ObjID,
capMaybeAsid :: Maybe Asid }
| PML4Cap {
capObj :: ObjID,
capMaybeAsid :: Maybe Asid }
| PUDCap {
capObj :: ObjID,
capMaybeAsid :: Maybe Asid }
| PGDCap {
capObj :: ObjID,
capMaybeAsid :: Maybe Asid }
| ASIDControlCap -- only one ASIDTable in the system
| ASIDPoolCap {
capObj :: ObjID }
-- ARM specific caps
| ARMIOSpaceCap { capObj :: ObjID }
| ARMIRQHandlerCap { capObj :: ObjID }
| ARMSIDCap { capObj :: ObjID }
| ARMCBCap { capObj :: ObjID }
| ARMSMCCap {
capObj :: ObjID,
capBadge :: Word }
-- X86 specific caps
| IOPortsCap {
capObj :: ObjID}
| IOSpaceMasterCap -- can mint to any IOSpaceCap
| IOSpaceCap { capObj :: ObjID }
| IOPTCap { capObj :: ObjID }
deriving (Eq, Ord, Show)
-- Use Template Haskell to create Prisms corresponding to the Cap constructors.
-- In combination with "Control.Lens.Extras (is)" this allows us to easily write
-- things like "is _CNodeCap".
makePrisms ''Cap
-- Kernel Objects
type CapMap a = Map a Cap
data TCBExtraInfo = TCBExtraInfo {
ipcBufferAddr :: Word,
ip :: Maybe Word,
sp :: Maybe Word,
prio :: Maybe Integer,
max_prio :: Maybe Integer,
affin :: Maybe Integer,
resume :: Maybe Bool }
deriving (Eq, Show)
data SCExtraInfo = SCExtraInfo {
period :: Maybe Word64,
budget :: Maybe Word64,
scData :: Maybe Word }
deriving (Eq, Show)
--
-- Kernel objects in memory.
--
-- This type represents attributes associated with in-memory kernel
-- objects. The type parameter 'a' is the type used to name caps. (For
-- example, a C implementation would use a 32-bit word, while another
-- implementation may pre-decode caps in a list of words).
--
data KernelObject a
= Endpoint
| Notification
| TCB {
slots :: CapMap a,
faultEndpoint :: Maybe Word,
extraInfo :: Maybe TCBExtraInfo,
dom :: Integer,
initArguments :: [Word] }
| CNode {
slots :: CapMap a,
sizeBits :: Word }
| Untyped {
maybeSizeBits :: Maybe Word,
maybePaddr :: Maybe Word }
| SC {
sc_extraInfo :: Maybe SCExtraInfo,
maybeSizeBits :: Maybe Word}
| RTReply
| VCPU
-- arch specific objects, ARM11, IA32, X86_64, AARCH64 and RISCV mixed
| ASIDPool { slots :: CapMap a,
capAsidHigh :: Maybe Word }
| PT { slots :: CapMap a }
| PD { slots :: CapMap a }
| PDPT { slots :: CapMap a }
| PML4 { slots :: CapMap a }
| PUD { slots :: CapMap a }
| PGD { slots :: CapMap a }
| Frame {
vmSizeBits :: Word,
maybePaddr :: Maybe Word,
maybeFill :: Maybe [[String]] }
-- ARM specific objects
| ARMIODevice {
slots :: CapMap a,
armiospace :: Word}
| ARMIrq {
slots :: CapMap a,
trigger :: Word,
target :: Word }
-- fake kernel objects for smmu
| ARMSID
| ARMCB { bankNumber :: Maybe Word }
| ARMSMC
-- X86 specific objects
| IOPorts { ports :: (Word, Word) }
| IODevice {
slots :: CapMap a,
domainID :: Word,
pciDevice :: (Word, Word, Word)}
| IOPT {
slots :: CapMap a,
level :: Word }
| IOAPICIrq {
slots :: CapMap a,
ioapic :: Word,
ioapic_pin :: Word,
ioapic_level :: Word,
ioapic_polarity :: Word }
| MSIIrq {
slots :: CapMap a,
handle :: Word,
bus :: Word,
dev :: Word,
fun :: Word }
deriving (Eq, Show)
objPaddr :: KernelObject a -> Maybe Word
objPaddr (Frame _ paddr _) = paddr
objPaddr (Untyped _ paddr) = paddr
objPaddr _ = Nothing
data KOType
= Endpoint_T
| Notification_T
| TCB_T
| CNode_T
| Untyped_T
| IrqSlot_T
| IOAPICIrqSlot_T
| MSIIrqSlot_T
| ARMIrqSlot_T
| ARMSID_T
| ARMCB_T
| ARMSMC_T
| ASIDPool_T
| PT_T
| PD_T
| PDPT_T
| PML4_T
| PUD_T
| PGD_T
| Frame_T
| IOPorts_T
| IODevice_T
| ARMIODevice_T
| IOPT_T
| VCPU_T
| SC_T
| RTReply_T
deriving (Show, Eq, Ord, Enum)
-- Lookup table of object sizes, to be passed in externally.
type ObjectSizeMap = Map KOType Word
--
-- A reference to a capability.
--
-- The ObjID is the kernel object the capability sits within (which will
-- be either a CNode or a TCB), and the Word represents the slot
-- (indexed from 0).
--
type CapRef = (ObjID, Word)
-- The name of a cap, used when copying caps.
type CapName = ObjID
type ObjMap a = Map ObjID (KernelObject a)
type IRQMap = Map Word ObjID
-- Map from untypeds to the lists of objects they cover.
-- Note that we use lists to preserve ordering. The Python capDL
-- allocator uses ordering to describe where objects are to be allocated
-- within each untyped.
--
-- This list may contain duplicate IDs while parsing; see
-- 'CapDL.MakeModel.dedupCoverIDs'.
type CoverMap = Map ObjID [ObjID]
-- UTs without cover decls aren't recorded, so return '[]' for those
getUTCover :: ObjID -> CoverMap -> [ObjID]
getUTCover = Map.findWithDefault []
type CDT = Map CapRef CapRef
--
-- The state of the system.
--
-- The system state consists of:
--
-- 1. The architecture in use;
-- 2. The objects currently present;
-- 3. The global irq node; and
-- 4. The cap derivation tree (which allows us to determine which
-- objects are derived from which other objects)
--
-- Two forms of model exist. The first is an abstract model where
-- CSpaces are assumed to be flat. Caps are identified by a Word
-- pointing somewhere in the CSpace.
--
-- The second model is a more concrete object where caps are assumed to
-- be in a tree of CNodes. Caps are identified by a list of Words
-- indicating the offsets of the target cap in each level of the tree.
--
data Model a
= Model {
arch :: Arch,
objects :: ObjMap a,
irqNode :: IRQMap,
cdt :: CDT,
untypedCovers :: CoverMap }
deriving Show
data Idents cap_id = Idents {
cap_ids :: Map cap_id CapRef
} deriving Show
type CopyMap = Map CapRef CapName
--
-- Each TCB contains several cap slots. The following constants define the
-- slot numbers in which they will be found if the TCB is treated as
-- a CNode.
--
tcbCTableSlot :: Word
tcbCTableSlot = 0
tcbVTableSlot :: Word
tcbVTableSlot = 1
tcbReplySlot :: Word
tcbReplySlot = 2
tcbCallerSlot :: Word
tcbCallerSlot = 3
tcbIPCBufferSlot :: Word
tcbIPCBufferSlot = 4
tcbFaultEPSlot :: Word
tcbFaultEPSlot = 5
tcbSCSlot :: Word
tcbSCSlot = 6
tcbTempFaultEPSlot :: Word
tcbTempFaultEPSlot = 7
tcbBoundVCPUSlot :: Word
tcbBoundVCPUSlot = 8
--
-- The string used when defining an IOSpaceMasterCap, an ASIDControlCap,
-- an IRQControlCap, a DomainCap or a SchedControlCap.
--
ioSpaceMaster :: String
ioSpaceMaster = "io_space_master"
asidControl :: String
asidControl = "asid_control"
irqControl :: String
irqControl = "irq_control"
domain :: String
domain = "domain"
schedControl :: String
schedControl = "sched_control"
capStrings :: [String]
capStrings = [ioSpaceMaster, asidControl, irqControl, domain, schedControl]
--
-- Determine if the given capability points to an object.
--
hasObjID :: Cap -> Bool
hasObjID NullCap = False
hasObjID IOSpaceMasterCap = False
hasObjID ASIDControlCap = False
hasObjID IRQControlCap = False
hasObjID DomainCap = False
hasObjID (SchedControlCap {}) = False
hasObjID _ = True
--
-- Get the object a particular cap points to.
--
-- This function is partial, not all caps point to an object.
--
objID :: Cap -> ObjID
objID = capObj
--
-- Determine if the given cap has rights.
--
hasRights :: Cap -> Bool
hasRights (NotificationCap {}) = True
hasRights (EndpointCap {}) = True
hasRights (ARMSMCCap {}) = True
hasRights (FrameCap {}) = True
hasRights _ = False
--
-- Determine if the given object has capability slots.
--
hasSlots :: KernelObject a -> Bool
hasSlots (TCB {}) = True
hasSlots (CNode {}) = True
hasSlots (ASIDPool {}) = True
hasSlots (PT {}) = True
hasSlots (PD {}) = True
hasSlots (PDPT {}) = True
hasSlots (PML4 {}) = True
hasSlots (PUD {}) = True
hasSlots (PGD {}) = True
hasSlots (ARMIODevice {}) = True
hasSlots (ARMIrq {}) = True
hasSlots (IODevice {}) = True
hasSlots (IOPT {}) = True
hasSlots (IOAPICIrq {}) = True
hasSlots (MSIIrq {}) = True
hasSlots _ = False