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
|
"""libos port receive-path model tests (sim/libos_port.py, docs/51 section 7).
Hermetic: a fake machine (register file only) and a fake base syscall model;
no image, no boot, no machine. The tests pin the port-syscall ABI observed on
the real 570.144 image (send args a0-a3, receive args a4-a7, wait id t1,
timeout t2, return a0/a4/a5/a6), the async receive post, the boot handshake
send, the host doorbell wake's external-interrupt handoff, and the strict
"never complete an uncompleted receive" rule.
Offline only: no hardware, kernel module, mdev, guest, network or QEMU.
Run::
cd open-iov/debug/risc-v-gsp-rm-vm
python3 -m unittest tests.test_libos_port -v
"""
from __future__ import annotations
import json
import os
import sys
import unittest
HERE = os.path.dirname(os.path.abspath(__file__))
TOOL_ROOT = os.path.abspath(os.path.join(HERE, ".."))
if TOOL_ROOT not in sys.path:
sys.path.insert(0, TOOL_ROOT)
from sim.libos import INTERRUPT_ARM_EXTERNAL, TASK_PORT_SEND_RECV_WAIT # noqa: E402
from sim.libos_port import ( # noqa: E402
EXTINTR_WAKE_CONTRACT,
HANDSHAKE_CONTRACT,
REPORT_FORMAT,
SHUTTLE_ANY,
TIMEOUT_INFINITE,
LibosPortModel,
role_for,
)
class FakeMachine:
"""Register file only: the port model never touches memory."""
def __init__(self):
self.x = [0] * 32
self.insns = 0
self.pc = 0
def set(self, index: int, value: int) -> None:
self.x[index] = value & 0xFFFFFFFFFFFFFFFF
class FakeBase:
"""Minimal base syscall model: handles arm, records and stops the rest."""
name = "fake-libos"
def __init__(self):
self.calls: list[int] = []
self.armed_mask = 0
self.stopped: dict | None = None
def handle(self, machine, pc: int = 0):
number = int(machine.x[5])
if number == INTERRUPT_ARM_EXTERNAL:
self.armed_mask |= int(machine.x[10])
return True, 0
self.calls.append(number)
self.stopped = {"num": number, "name": f"sys_{number}", "pc": int(pc)}
return False, None
def _port_call(machine, *, send=None, recv=None, wait=None,
timeout=TIMEOUT_INFINITE) -> None:
"""Write one ``TASK_PORT_SEND_RECV_WAIT`` argument set (explicit fields)."""
machine.set(5, TASK_PORT_SEND_RECV_WAIT) # t0
for index, value in enumerate(send or (0, 0, 0, 0), start=10):
machine.set(index, value)
for index, value in enumerate(recv or (0, 0, 0, 0), start=14):
machine.set(index, value)
machine.set(6, 0 if wait is None else wait) # t1
machine.set(7, timeout) # t2
class PortSurfaceTests(unittest.TestCase):
"""Role table and argument contract."""
def test_observed_roles(self):
self.assertEqual(role_for(6), "extintr")
self.assertEqual(role_for(7), "notification")
self.assertEqual(role_for(8), "request")
self.assertEqual(role_for(12), "handshake")
self.assertEqual(role_for(13), "event")
self.assertEqual(role_for(99), "data")
def test_async_post_keeps_receive_args_and_returns_ok(self):
model = LibosPortModel(base=FakeBase())
machine = FakeMachine()
_port_call(machine, recv=(10, 6, 0, 0))
handled, result = model.handle(machine, pc=0x4018038)
self.assertTrue(handled)
self.assertEqual(result, 0)
self.assertEqual(len(model.posts), 1)
self.assertEqual(model.posts[0]["shuttle"], 10)
self.assertEqual(model.posts[0]["role"], "extintr")
# The kernel returns LIBOS_OK through a0; the wrapper masks a0 only.
self.assertEqual(machine.x[14], 10)
self.assertEqual(machine.x[15], 6)
def test_send_completes_with_the_transcribe_contract(self):
model = LibosPortModel(base=FakeBase())
machine = FakeMachine()
_port_call(machine, send=(1, 12, 0, 0, ), wait=1)
handled, result = model.handle(machine, pc=0x4019CD4)
self.assertTrue(handled)
self.assertEqual(result, 0)
self.assertEqual(len(model.sends), 1)
self.assertEqual(model.sends[0]["contract"], HANDSHAKE_CONTRACT)
self.assertEqual(model.sends[0]["role"], "handshake")
self.assertEqual(machine.x[14], 0) # completed size
self.assertEqual(machine.x[15], 0) # originating task
self.assertEqual(machine.x[16], 1) # completed shuttle
self.assertEqual(machine.x[17], 0)
def test_blocking_wait_stops_without_completion(self):
model = LibosPortModel(base=FakeBase())
machine = FakeMachine()
_port_call(machine, recv=(9, 8, 0x4015070, 0x808))
model.handle(machine, pc=0x4018038)
_port_call(machine, wait=SHUTTLE_ANY)
handled, result = model.handle(machine, pc=0x4019DF4)
self.assertFalse(handled)
self.assertIsNone(result)
self.assertIsNotNone(model.stopped)
self.assertEqual(model.stopped["reason"], "port-wait-blocks")
self.assertEqual(model.stopped["num"], TASK_PORT_SEND_RECV_WAIT)
self.assertEqual(model.waits[-1]["reason"], "port-wait-blocks")
def test_unmodelled_syscall_is_recorded_and_stopped(self):
base = FakeBase()
model = LibosPortModel(base=base)
machine = FakeMachine()
machine.set(5, 6) # task_state_query
machine.set(10, 0)
handled, result = model.handle(machine, pc=0x401A46C)
self.assertFalse(handled)
self.assertIsNone(result)
self.assertEqual(model.stopped["num"], 6)
self.assertEqual(model.stopped["reason"], "unmodelled-syscall")
self.assertEqual(base.calls, [6])
class DoorbellWakeTests(unittest.TestCase):
"""The host wake's external-interrupt handoff (RTOS report contract)."""
def test_kick_before_post_and_arm_is_delivered_at_post(self):
base = FakeBase()
model = LibosPortModel(base=base)
machine = FakeMachine()
event = model.host_kick(queue=0, value=0)
self.assertIsNone(event["delivered"])
self.assertTrue(event["pending"])
# Arm first, then post the extintr receive: the pending wake fills it.
machine.set(5, INTERRUPT_ARM_EXTERNAL)
machine.set(10, 0xFF)
model.handle(machine, pc=0x4018054)
_port_call(machine, recv=(10, 6, 0, 0))
model.handle(machine, pc=0x4018038)
self.assertEqual(model.kicks_pending, 0)
self.assertEqual([entry["shuttle"] for entry in model.completed], [10])
self.assertEqual(model.completed[0]["payload_size"], 0)
self.assertEqual(model.completed[0]["origin_task"], 0)
self.assertEqual(model.completed[0]["contract"], EXTINTR_WAKE_CONTRACT)
def test_wake_requires_an_arm(self):
model = LibosPortModel(base=FakeBase())
machine = FakeMachine()
_port_call(machine, recv=(10, 6, 0, 0))
model.handle(machine, pc=0x4018038)
event = model.host_kick()
self.assertIsNone(event["delivered"])
self.assertEqual(model.kicks_pending, 1)
self.assertEqual(model.completed, [])
# Without interrupts armed the wait still blocks (no fabricated wake).
_port_call(machine, wait=SHUTTLE_ANY)
handled, _ = model.handle(machine, pc=0x4019DF4)
self.assertFalse(handled)
def test_arm_seeds_from_the_attached_base_model(self):
base = FakeBase()
base.armed_mask = 0xFF
model = LibosPortModel(base=base)
self.assertEqual(model.armed_mask, 0xFF)
machine = FakeMachine()
model.host_kick()
_port_call(machine, recv=(10, 6, 0, 0))
model.handle(machine, pc=0x4018038)
self.assertEqual(model.kicks_pending, 0)
self.assertEqual([entry["shuttle"] for entry in model.completed], [10])
def test_wait_returns_the_wake_with_the_argument_contract(self):
model = LibosPortModel(base=FakeBase())
machine = FakeMachine()
model.host_kick()
_port_call(machine, recv=(10, 6, 0, 0))
model.handle(machine, pc=0x4018038)
machine.set(5, INTERRUPT_ARM_EXTERNAL)
machine.set(10, 0xFF)
model.handle(machine, pc=0x4018054)
_port_call(machine, wait=SHUTTLE_ANY)
handled, result = model.handle(machine, pc=0x4019DF4)
self.assertTrue(handled)
self.assertEqual(result, 0)
self.assertEqual(machine.x[14], 0) # completed size
self.assertEqual(machine.x[15], 0) # originating task
self.assertEqual(machine.x[16], 10) # completed shuttle
self.assertEqual(model.waits[-1]["completed"], 10)
self.assertEqual(model.waits[-1]["role"], "extintr")
def test_one_wake_completes_one_receive_only(self):
model = LibosPortModel(base=FakeBase())
machine = FakeMachine()
model.host_kick()
_port_call(machine, recv=(10, 6, 0, 0))
model.handle(machine, pc=0x4018038)
machine.set(5, INTERRUPT_ARM_EXTERNAL)
machine.set(10, 0xFF)
model.handle(machine, pc=0x4018054)
self.assertEqual(len(model.completed), 1)
# Consume the wake, re-post and wait again: nothing completes.
_port_call(machine, wait=SHUTTLE_ANY)
handled, _ = model.handle(machine, pc=0x4019DF4)
self.assertTrue(handled)
_port_call(machine, recv=(10, 6, 0, 0))
model.handle(machine, pc=0x4018C8)
_port_call(machine, wait=SHUTTLE_ANY)
handled, _ = model.handle(machine, pc=0x4019DF4)
self.assertFalse(handled)
self.assertEqual(model.completed, [])
class StateTests(unittest.TestCase):
"""Deterministic, serialisable report state."""
def _run(self) -> dict:
model = LibosPortModel(base=FakeBase())
machine = FakeMachine()
model.host_kick(queue=0, value=0)
_port_call(machine, recv=(10, 6, 0, 0))
model.handle(machine, pc=0x4018038)
machine.set(5, INTERRUPT_ARM_EXTERNAL)
machine.set(10, 0xFF)
model.handle(machine, pc=0x4018054)
_port_call(machine, recv=(9, 8, 0x4015070, 0x808))
model.handle(machine, pc=0x4018038)
_port_call(machine, wait=SHUTTLE_ANY)
model.handle(machine, pc=0x4019DF4)
return model.state()
def test_state_is_byte_identical_across_runs(self):
first = self._run()
second = self._run()
self.assertEqual(first["format"], REPORT_FORMAT)
self.assertEqual(json.dumps(first, sort_keys=True),
json.dumps(second, sort_keys=True))
self.assertEqual(first["armed_mask"], 0xFF)
self.assertEqual(first["host_kicks"], 1)
self.assertEqual(first["kicks_pending"], 0)
self.assertEqual([post["role"] for post in first["posts"]],
["extintr", "request"])
self.assertEqual(first["completed"], [])
self.assertEqual(first["waits"][-1]["completed"], 10)
def test_state_has_no_host_timestamps(self):
state = self._run()
def keys(value):
if isinstance(value, dict):
for key, item in value.items():
yield key
yield from keys(item)
elif isinstance(value, list):
for item in value:
yield from keys(item)
for key in keys(state):
self.assertNotIn(str(key), ("t", "ts", "timestamp", "time"))
if __name__ == "__main__":
unittest.main()
|