Skip to content

Commit e0adc3d

Browse files
committed
Add the things needed for the host to the demo.
1 parent 20c98ee commit e0adc3d

File tree

13 files changed

+450
-0
lines changed

13 files changed

+450
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
Morello machine setup
2+
=====================
3+
4+
This directory contains the files that are necessary to set up the Morello machine to act as the server in this demo.
5+
6+
Note: This contains the *private* key used on the server for the demo.
7+
This would allow anyone to impersonate the server.
8+
This does not matter because it is used *only* for the demo, never use this key for anything important!
9+
Including the key here remove the need to generate a new header file for the client portion of the demo.
10+
11+
Pure-capability packages:
12+
13+
minicom
14+
15+
Hybrid packages:
16+
17+
bind918
18+
isc-dhcp44-server
19+
jq
20+
npm
21+
wireshark
22+
23+
Built from source:
24+
25+
cheriot-audit (no port yet)
26+
mosquitto (xsltproc is broken and the port's no-docs mode doesn't work).
27+
28+
Make sure to build Release builds (-O0 is *really* slow on Morello, with -O0 Mosquitto can't keep up with two clients on FPGA!).
29+
Install in /opt.
30+
31+
The following lines need to be added to /etc/rc.conf:
32+
33+
```
34+
# Network interface for the demo
35+
ifconfig_ue0="inet 10.0.0.10 netmask 255.0.0.0"
36+
37+
# DHCP server
38+
dhcpd_enable="YES" # dhcpd enabled?
39+
dhcpd_ifaces="ue0" # ethernet interface(s)
40+
dhcpd_withumask="022" # file creation mask
41+
42+
# bind
43+
named_enable="YES"
44+
45+
# NTP
46+
ntpd_enable="YES"
47+
48+
# Mosquitto
49+
mosquitto_enable="YES"
50+
51+
devfs_enable="YES"
52+
```
53+
54+
Setting up DHCP
55+
---------------
56+
57+
The first thing that the demo will do is try to get a DHCP lease.
58+
This requires dhcpd to listen in the demo ethernet adaptor (configured in `rc.conf`) and to provide the host IP (10.0.0.10) as the DNS server.
59+
The `usr/local64/etc/dhcpd.conf` file contains the configuration for the DHCP server and should be copied into `/usr/local64/etc/dhcpd.conf`.
60+
61+
Setting up DNS
62+
--------------
63+
64+
After acquiring a DHCP lease, the demo will try to look up host names via DNS.
65+
For disconnected operation, we will fake the two DNS names (pool.ntp.org and cheriot.demo) by configuring the DNS server to be authoritative for these zones.
66+
Add the following lines to the end of `/usr/local64/etc/namedb/named.conf`:
67+
68+
```
69+
zone "cheriot.demo" {
70+
type master;
71+
file "/usr/local64/etc/namedb/db.cheriot.demo";
72+
};
73+
74+
zone "pool.ntp.org" {
75+
type master;
76+
file "/usr/local64/etc/namedb/db.pool.ntp.org";
77+
};
78+
```
79+
80+
Then copy the `db.cheriot.demo` and `db.pool.ntp.org` files from `usr/local64/etc/namedb` to `/usr/local64/etc/namedb/`.
81+
82+
Setting up NTP
83+
--------------
84+
85+
For disconnected operation, the NTP server needs to be configured to lie and pretend that it is an authoritative server when it can't sync with a real NTP server.
86+
The following lines in /etc/ntp.conf will do this:
87+
88+
```
89+
server 127.127.1.0 prefer
90+
fudge 127.127.1.0 #stratum 10
91+
```
92+
93+
Note: It would be better to use `tos orphan 4`, but this defaults to a 10-minute timeout before deciding to become authoritative and this needs to be dropped to a few seconds.
94+
95+
Setting up Mosquitto
96+
--------------------
97+
98+
The Mosquitto MQTT server configuration is in `opt/etc/mosquitto/`.
99+
Copy these files into `/opt/etc/mosquitto/`.
100+
You can also copy the [rc script](https://github.com/freebsd/freebsd-ports/blob/main/net/mosquitto/files/mosquitto.in) from the port into `/usr/local/etc/rc.d/mosquitto` (replace `%%PREFIX%%` with `/opt`).
101+
Alternatively, you can just start mosquitto manually and run it in the foreground.
102+
103+
Wireshark
104+
---------
105+
106+
To inspect the packets, use Wireshark.
107+
This requires that the demo user has access to the `bpf` device.
108+
The easiest way of doing this is to add the user to a group called `bpf` and add the following to `/etc/devfs.conf`:
109+
110+
```
111+
own bpf root:bpf
112+
perm bpf 660
113+
```
114+
115+
Console UART
116+
------------
117+
118+
The `home/demo/.minirc.dfl` file contains the configuration for minicom to connect to the FPGA.
119+
Run minicom as `minicom -c on -D /dev/ttyU1` or `minicom -c on -D /dev/ttyU3` to connect to the FPGA.
120+
The demo user will need to have access to the USB TTY devices.
121+
The easiest way to do this is to add the user to the `dialer` group and add the following to `/etc/devfs.conf`:
122+
123+
```
124+
own ttyU* root:dialer
125+
perm ttyU* 660
126+
```
127+
128+
Note that each FPGA has two FDTI devices, you need to use the *odd* numbered ones.
129+
130+
Driving the demo
131+
----------------
132+
133+
The auditing portions of the demo are driven by the `audit.sh` script in `home/demo`.
134+
Drop this in a directory along with the board description JSON and the firmware JSON from the final build.
135+
136+
The script to push new JavaScript, and an example JavaScript file, for the demo are in: `home/demo/script`
137+
The `cheri.js` file here is the host interfaces, people may wish to modify `demo.js` to show dynamic code updates.
138+
Note: MQTT does not do caching, so you must push out the JavaScript each time a new client connects.
139+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Machine-generated file - use setup menu in minicom to change parameters.
2+
pu baudrate 115200
3+
pu bits 8
4+
pu parity N
5+
pu stopbits 1
6+
pu rtscts No
7+
pu addlinefeed No
8+
pu linewrap Yes
9+
pu addcarreturn Yes
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
if [ $# -eq 0 ] ; then
4+
echo Query required. Try one of the following:
5+
echo Print all connection capabilities:
6+
echo -e \\tdata.network_stack.all_connection_capabilities
7+
echo Is the network stack configuration valid?
8+
echo -e "\\t'data.network_stack.valid(kunyan_ethernet)'"
9+
echo Print all allocator capabilities and their owners:
10+
echo -e "\\t'[ { \"owner\": owner, \"capability\": data.rtos.decode_allocator_capability(c) } | c = input.compartments[owner].imports[_] ; data.rtos.is_allocator_capability(c) ]'"
11+
echo Print all compartments that invoke functions in the JavaScript compartment:
12+
echo -e "\\t'data.compartment.compartments_calling(\"javascript\")'"
13+
echo Print all compartments that invoke functions in the allocator:
14+
echo -e "\\t'data.compartment.compartments_calling(\"allocator\")'"
15+
echo Print all compartments that have direct access to the LEDs / switches:
16+
echo -e "\\t'data.compartment.compartments_with_mmio_import(data.board.devices.gpio_led0)'"
17+
else
18+
echo "cheriot-audit --board ibex-arty-a7-100.json --firmware-report cheritech-demo.json --module network_stack.rego --query \"$1\""
19+
cheriot-audit --board ibex-arty-a7-100.json --firmware-report cheritech-demo.json --module network_stack.rego --query "$1" | jq
20+
fi
21+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// FFI Imports
2+
// Each function imported from the host environment needs to be assigned to a
3+
// global like this and identified by a constant that the resolver in the C/C++
4+
// code will understand.
5+
// These constants are defined in the `Exports` enumeration.
6+
7+
8+
var FFINumber = 1;
9+
10+
/**
11+
* Log function, writes all arguments to the UART.
12+
*/
13+
export const print = vmImport(FFINumber++);
14+
15+
/**
16+
* led_on(index).
17+
*
18+
* Turns on the LED at the specified index.
19+
*/
20+
export const led_on = vmImport(FFINumber++);
21+
22+
/**
23+
* led_off(index).
24+
*
25+
* Turns off the LED at the specified index.
26+
*/
27+
export const led_off = vmImport(FFINumber++);
28+
29+
/**
30+
* buttons_read().
31+
*
32+
* Reads the value of all of the buttons, returning a 4-bit value indicating
33+
* the states of all of them.
34+
*/
35+
export const buttons_read = vmImport(FFINumber++);
36+
37+
/**
38+
* switches_read().
39+
*
40+
* Reads the value of all of the switches, returning a 4-bit value indicating
41+
* the states of all of them.
42+
*/
43+
export const switches_read = vmImport(FFINumber++);
44+
45+
46+
export const mqtt_publish = vmImport(FFINumber++);
47+
export const mqtt_subscribe = vmImport(FFINumber++);
48+
49+
/**
50+
* led_set(index, state).
51+
*
52+
* Turns the LED at the specified index on or off depending on whether state is
53+
* true or false.
54+
*/
55+
export function led_set(index, state)
56+
{
57+
if (state)
58+
{
59+
led_on(index);
60+
}
61+
else
62+
{
63+
led_off(index);
64+
}
65+
}
66+
67+
/**
68+
* button_read(index).
69+
*
70+
* Reads the value of the button at the specified index.
71+
*/
72+
export function button_read(index)
73+
{
74+
return (buttons_read() & (1 << index)) !== 0;
75+
}
76+
77+
78+
/**
79+
* switch_read(index).
80+
*
81+
* Reads the value of the switch at the specified index.
82+
*/
83+
export function switch_read(index)
84+
{
85+
return (switches_read() & (1 << index)) !== 0;
86+
}
87+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
set -e
3+
microvium demo.js
4+
echo Publishing code to MQTT broker
5+
mosquitto_pub -h cheriot.demo -p 8883 --cafile /opt/etc/mosquitto/certs/cert.pem -t cheri-code -f demo.mvm-bc
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import * as host from "./cheri.js"
2+
3+
var ticks = 0
4+
var switches = 0
5+
6+
/**
7+
* Subscribe to a topic, print to the UART whether the subscription was
8+
* successful.
9+
*/
10+
function subscribe(topic)
11+
{
12+
var ret = host.mqtt_subscribe(topic)
13+
host.print("Subscribe ", topic, " returned: ", ret)
14+
if (ret)
15+
{
16+
host.print("Subscribed to", topic)
17+
}
18+
else
19+
{
20+
host.print("Failed to subscribe to ", topic)
21+
}
22+
}
23+
24+
/**
25+
* On first run, subscribe to the switch topics.
26+
*/
27+
function first_run()
28+
{
29+
subscribe("cheri-switch-0")
30+
subscribe("cheri-switch-1")
31+
}
32+
33+
/**
34+
* Tick function, called every 100ms (roughly).
35+
*/
36+
function tick()
37+
{
38+
if (ticks === 0)
39+
{
40+
first_run();
41+
}
42+
ticks++
43+
// If we're not a lightswitch, don't do anything else.
44+
if (host.switch_read(3))
45+
{
46+
return;
47+
}
48+
// If we're not a lightbulb, make sure the lights are out
49+
host.led_off(0)
50+
host.led_off(1)
51+
// Uncomment the next block to validate that the tick callback is being called.
52+
/*
53+
if (ticks % 5 === 0)
54+
{
55+
host.print("tick: ", ticks)
56+
}
57+
*/
58+
var new_switches = host.switches_read()
59+
if (new_switches !== switches)
60+
{
61+
for (var i = 0 ; i < 2 ; i++)
62+
{
63+
if ((new_switches & (1 << i)) !== (switches & (1 << i)))
64+
{
65+
host.print("Switch ", i, " changed to ", (new_switches & (1 << i)) ? "on" : "off")
66+
host.mqtt_publish("cheri-switch-" + i, (new_switches & (1 << i)) ? "on" : "off")
67+
}
68+
}
69+
switches = new_switches
70+
}
71+
}
72+
73+
/**
74+
* Publish notification callback, called whenever a new publish message is
75+
* received from the MQTT broker.
76+
*/
77+
function message(topic, message)
78+
{
79+
host.print("Received message on topic: ", topic, " message: ", message)
80+
var switchNumber = -1
81+
// If we're not a lightbulb, don't do anything else.
82+
if (!host.switch_read(3))
83+
{
84+
return;
85+
}
86+
if (topic === "cheri-switch-0")
87+
{
88+
switchNumber = 0
89+
}
90+
else if (topic === "cheri-switch-1")
91+
{
92+
switchNumber = 1
93+
}
94+
else
95+
{
96+
return
97+
}
98+
if (message === "on")
99+
{
100+
host.led_on(switchNumber)
101+
}
102+
else
103+
{
104+
host.led_off(switchNumber)
105+
}
106+
}
107+
108+
vmExport(1234, tick);
109+
vmExport(1235, message);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBgzCCASmgAwIBAgIUeyRaxt/cqeeZ1JByg4V4shx4lhowCgYIKoZIzj0EAwIw
3+
FzEVMBMGA1UEAwwMY2hlcmlvdC5kZW1vMB4XDTI0MDQwODE0NTcwMVoXDTI1MDQw
4+
ODE0NTcwMVowFzEVMBMGA1UEAwwMY2hlcmlvdC5kZW1vMFkwEwYHKoZIzj0CAQYI
5+
KoZIzj0DAQcDQgAE2zq+r59p+QKkoKdBguXxBl4KoX5DYb6gHyI9Wrn7o4bz8rNZ
6+
4JPG4J+mIlEQKv9eIJYn1owIWQ5YbKaHpZqWAqNTMFEwHQYDVR0OBBYEFBdDvYEz
7+
T9pLdHbNwBVFT9wwQGVdMB8GA1UdIwQYMBaAFBdDvYEzT9pLdHbNwBVFT9wwQGVd
8+
MA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDSAAwRQIgb2epifZyBtLofZsk
9+
gs5HqfpKuiMijfe3Q+H7ETP3aIwCIQDYBIR7uQ4s24mK3dcj+u5Qc6gSr/WuBZGO
10+
xzxrtzDGTw==
11+
-----END CERTIFICATE-----
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgFF2t/aeGHzLHsP4k
3+
63Q9yIFLeU8+mtOylWjhfwwQbNihRANCAATbOr6vn2n5AqSgp0GC5fEGXgqhfkNh
4+
vqAfIj1aufujhvPys1ngk8bgn6YiURAq/14glifWjAhZDlhspoelmpYC
5+
-----END PRIVATE KEY-----

0 commit comments

Comments
 (0)