forked from keystone-enclave/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
332 lines (282 loc) · 13.6 KB
/
CMakeLists.txt
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
cmake_minimum_required(VERSION 3.5)
project(keystone C)
include(ProcessorCount)
set(CMAKE_VERBOSE_MAKEFILE ON)
###############################################################################
## MACROS
###############################################################################
# add patch macro
macro(add_patch submodule patch working_directory patch_list)
add_custom_command(OUTPUT ${patch}.applied
WORKING_DIRECTORY ${working_directory}
COMMAND patch --forward -p0 < ${patchdir}/${submodule}/${patch} || true
COMMAND touch ${CMAKE_BINARY_DIR}/${patch}.applied
COMMENT "Applying ${patch}")
list(APPEND ${patch_list} ${patch}.applied)
endmacro()
macro(mkdir name dir)
add_custom_command(OUTPUT ${dir}/.exists
COMMAND mkdir -p ${dir}
COMMAND touch ${dir}/.exists
)
set(${name} ${dir})
set(${name}_exists ${dir}/.exists)
endmacro()
###############################################################################
## CONFIGURATION/VARIABLES
###############################################################################
if(NOT DEFINED ENV{RISCV})
message(FATAL_ERROR "set RISCV environment variable. \n Try `cd ${CMAKE_SOURCE_DIR}; source source.sh`")
endif()
set(USE_RUST_SM FALSE CACHE BOOL "Use Rust version of the security monitor.")
set(SM_CONFIGURE_ARGS --enable-opt=2 CACHE STRING "Security Monitor configure script arguments")
set(SM_PLATFORM "generic" CACHE STRING "Board name for SM hardware-specific functions")
set(platform ${SM_PLATFORM})
message(STATUS "platform=${platform}")
set(LINUX_SIFIVE FALSE CACHE BOOL "Build linux for sifive")
set(sifive ${LINUX_SIFIVE})
message(STATUS "sifive=${sifive}")
if((CMAKE_BUILD_TYPE MATCHES "Debug") OR (CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo"))
message(STATUS "Using debug symbols")
set(CFLAGS -g)
endif()
set(RISCV32 OFF CACHE BOOL "Build in 32-bit mode")
if(RISCV32)
message(STATUS "riscv32")
set(BITS 32)
set(ABI ilp32d)
else()
message(STATUS "riscv64")
set(BITS 64)
set(ABI lp64d)
endif()
set(ISA rv${BITS}imafdc)
set(confdir ${CMAKE_SOURCE_DIR}/conf)
set(patchdir ${CMAKE_SOURCE_DIR}/patches)
set(cross_compile riscv${BITS}-unknown-linux-gnu-)
set(bootrom_srcdir ${CMAKE_SOURCE_DIR}/bootrom)
set(bootrom_wrkdir ${CMAKE_BINARY_DIR}/bootrom.build)
set(qemu_wrkdir ${CMAKE_SOURCE_DIR}/qemu)
set(qemu_srcdir ${CMAKE_SOURCE_DIR}/qemu)
set(sm_srcdir ${CMAKE_SOURCE_DIR}/sm)
mkdir(sm_wrkdir ${CMAKE_BINARY_DIR}/sm.build)
set(buildroot_srcdir ${CMAKE_SOURCE_DIR}/buildroot)
set(buildroot_wrkdir ${CMAKE_BINARY_DIR}/buildroot.build)
set(buildroot_config ${confdir}/qemu_riscv${BITS}_virt_defconfig)
set(overlay_dir ${CMAKE_BINARY_DIR}/overlay)
set(overlay_root ${overlay_dir}/root)
if(firesim)
message(STATUS "Using Linux defconfig: ${linux_defconfig}")
set(initramfs true)
elseif(sifive)
message(STATUS "SiFive Unleashed configs and patches. Forcing initramfs=y")
set(linux_defconfig ${confdir}/linux64-sifive-defconfig)
message(STATUS "Using Linux defconfig: ${linux_defconfig}")
set(initramfs true)
endif()
if(NOT DEFINED linux_defconfig)
set(linux_defconfig ${confdir}/linux${BITS}-defconfig)
endif()
set(linux_srcdir ${CMAKE_SOURCE_DIR}/linux)
mkdir(linux_wrkdir ${CMAKE_BINARY_DIR}/linux.build)
set(linux_symvers ${linux_wrkdir}/Modules.symvers)
set(linux_image ${linux_wrkdir}/arch/riscv/boot/Image)
set(driver_srcdir ${CMAKE_SOURCE_DIR}/linux-keystone-driver)
set(driver_wrkdir ${CMAKE_BINARY_DIR}/linux-keystone-driver.build)
set(fw_elf ${sm_wrkdir}/platform/${platform}/firmware/fw_payload.elf)
set(fw_bin ${sm_wrkdir}/platform/${platform}/firmware/fw_payload.bin)
set(final_image ${CMAKE_BINARY_DIR}/firmware.bin)
set(initramfs_sysroot ${CMAKE_BINARY_DIR}/initramfs-sysroot)
###############################################################################
## COMPONENT: QEMU
###############################################################################
set(qemu_system ${qemu_wrkdir}/riscv${BITS}-softmmu/qemu-system-riscv${BITS})
add_patch("qemu" "qemu-secure-boot.patch" ${qemu_srcdir} qemu_patches)
add_custom_target("qemu" ALL DEPENDS ${qemu_system})
add_custom_command(OUTPUT ${qemu_system}
COMMAND $(MAKE) -C ${qemu_srcdir}
DEPENDS ${qemu_wrkdir}/config-host.mak ${qemu_patches}
COMMENT "Building QEMU"
)
add_custom_command(OUTPUT ${qemu_wrkdir}/config-host.mak
WORKING_DIRECTORY ${qemu_srcdir}
COMMAND ./configure --target-list=riscv${BITS}-softmmu,riscv${BITS}-linux-user
COMMENT "Configuring QEMU"
)
###############################################################################
## COMPONENT: buildroot
###############################################################################
add_custom_command(OUTPUT ${overlay_root} COMMAND mkdir -p ${overlay_root})
add_custom_command(OUTPUT ${overlay_dir} COMMAND mkdir -p ${overlay_dir})
add_custom_command(OUTPUT ${buildroot_wrkdir} COMMAND mkdir -p ${buildroot_wrkdir})
add_custom_target("buildroot" ALL DEPENDS ${buildroot_srcdir} ${buildroot_wrkdir}/.config ${overlay_root} ${buildroot_wrkdir}
COMMAND $(MAKE) -s -C ${buildroot_srcdir} RISCV=$ENV{RISCV} PATH=$ENV{PATH} O=${buildroot_wrkdir}
COMMENT "Building buildroot"
)
string(REPLACE "/" "\\/" overlay_dir_stripped ${overlay_dir})
add_custom_command(DEPENDS ${buildroot_config} OUTPUT ${buildroot_wrkdir}/.config
COMMAND mkdir -p ${buildroot_wrkdir}
COMMAND cp ${buildroot_config} ${buildroot_wrkdir}/.config
COMMAND sed \"s/^BR2_ROOTFS_OVERLAY=.*/BR2_ROOTFS_OVERLAY=\\\"${overlay_dir_stripped}\\\"/g\" -i ${buildroot_wrkdir}/.config
COMMAND $(MAKE) -s -C ${buildroot_srcdir} RISCV=$ENV{RISCV} PATH=$ENV{PATH}
O=${buildroot_wrkdir} CROSS_COMPILE=${cross_compile} olddefconfig
COMMENT "Configuring buildroot (overlay = ${overlay_dir})"
)
###############################################################################
## component: bootrom
###############################################################################
add_custom_command(OUTPUT ${bootrom_wrkdir} COMMAND mkdir -p ${bootrom_wrkdir})
add_custom_target("bootrom-sync" DEPENDS ${bootrom_srcdir} ${bootrom_wrkdir}
COMMAND rsync -r ${bootrom_srcdir}/ ${bootrom_wrkdir})
add_custom_target("bootrom" ALL DEPENDS ${bootrom_wrkdir} ${bootrom_srcdir} "bootrom-sync"
COMMAND $(MAKE) -C ${bootrom_wrkdir} O=${bootrom_wrkdir} march=${ISA} mabi=${ABI} CC=riscv${BITS}-unknown-elf-gcc
OBJCOPY=riscv${BITS}-unknown-elf-objcopy
COMMENT "Building bootrom"
)
###############################################################################
## COMPONENT: linux kernel
###############################################################################
add_custom_command(OUTPUT ${linux_wrkdir}/.config DEPENDS ${linux_defconfig} ${linux_wrkdir_exists}
COMMAND cp ${linux_defconfig} ${linux_wrkdir}/.config
COMMAND $(MAKE) -C ${linux_srcdir} O=${linux_wrkdir} ARCH=riscv olddefconfig
COMMENT "Configuring linux"
)
add_custom_target("linux-config" DEPENDS ${linux_wrkdir}/.config)
if(firesim)
add_patch("linux" "linux${BITS}.firesim.patch" ${linux_srcdir} linux_patches)
elseif(sifive)
add_patch("linux" "linux${BITS}.sifive.patch" ${linux_srcdir} linux_patches)
else()
add_patch("linux" "linux${BITS}.patch" ${linux_srcdir} linux_patches)
endif()
if(initramfs)
# linux-initramfs
execute_process(COMMAND id -u OUTPUT_VARIABLE uid)
string(STRIP ${uid} uid)
execute_process(COMMAND id -g OUTPUT_VARIABLE gid)
string(STRIP ${gid} gid)
add_custom_command(OUTPUT ${initramfs_sysroot} COMMAND mkdir -p ${initramfs_sysroot})
add_custom_command(OUTPUT ${linux_image} DEPENDS ${initramfs_sysroot} ${linux_srcdir} "linux-symvers" "buildroot" ${buildroot_wrkdir}/images/rootfs.tar
COMMAND tar -xpf ${buildroot_wrkdir}/images/rootfs.tar -C ${initramfs_sysroot} --exclude ./dev --exclude ./usr/share/locale
COMMAND echo "::sysinit:/bin/mount -t devtmpfs devtmpfs /dev" >> ${initramfs_sysroot}/etc/inittab
COMMAND $(MAKE) -C ${linux_srcdir}
O=${linux_wrkdir} CONFIG_INITRAMFS_SOURCE="${confdir}/initramfs.txt ${initramfs_sysroot}"
CONFIG_INITRAMFS_ROOT_UID=${uid} CONFIG_INITRAMFS_ROOT_GID=${gid}
CONFIG_DEVTMPFS=y CONFIG_DEVTMPFS_MOUNT=y
CROSS_COMPILE=${cross_compile} ARCH=riscv
COMMENT "Building linux (initramfs)"
)
else()
add_custom_command(OUTPUT ${linux_image} DEPENDS ${linux_srcdir} "linux-symvers"
COMMAND $(MAKE) -C ${linux_srcdir} O=${linux_wrkdir} CROSS_COMPILE=${cross_compile} ARCH=riscv
COMMENT "Building linux"
)
endif()
add_custom_command(OUTPUT ${linux_symvers} DEPENDS ${linux_srcdir} "linux-config" ${linux_patches}
COMMAND $(MAKE) -C ${linux_srcdir} O=${linux_wrkdir} CROSS_COMPILE=${cross_compile} ARCH=riscv modules
COMMENT "Building linux symvers"
)
add_custom_target("linux-symvers" DEPENDS ${linux_symvers})
add_custom_target("linux" ALL DEPENDS ${linux_image})
###############################################################################
## COMPONENT: linux driver
###############################################################################
add_custom_command(OUTPUT ${driver_wrkdir} COMMAND mkdir -p ${driver_wrkdir})
add_custom_target("driver-sync" DEPENDS ${driver_srcdir} ${driver_wrkdir}
COMMAND rsync -r ${driver_srcdir}/ ${driver_wrkdir})
add_custom_target("driver" ALL DEPENDS ${driver_srcdir} ${linux_srcdir} "linux-symvers" "driver-sync"
COMMAND $(MAKE) -C ${linux_wrkdir} O=${linux_wrkdir} CROSS_COMPILE=${cross_compile} ARCH=riscv
M=${driver_wrkdir} modules
COMMENT "Building driver"
)
###############################################################################
## COMPONENT: security monitor (sm)
###############################################################################
add_patch("sm/opensbi" "opensbi-firmware-secure-boot.patch" ${sm_srcdir}/opensbi sm_patches)
add_custom_target("sm" ALL DEPENDS "linux" ${sm_wrkdir_exists} ${sm_patches} WORKING_DIRECTORY ${sm_wrkdir}
COMMAND $(MAKE) -C ${sm_srcdir}/opensbi O=${sm_wrkdir} PLATFORM_DIR=${sm_srcdir}/plat/${platform}
CROSS_COMPILE=riscv${BITS}-unknown-elf- FW_PAYLOAD_PATH=${linux_image} FW_PAYLOAD=y PLATFORM_RISCV_XLEN=${BITS}
PLATFORM_RISCV_ISA=${ISA} PLATFORM_RISCV_ABI=${ABI}
COMMENT "Building sm"
)
###############################################################################
## COMPONENT: tests
###############################################################################
set(example_wrkdir examples)
add_subdirectory(sdk/examples ${example_wrkdir})
set_target_properties(examples PROPERTIES EXCLUDE_FROM_ALL YES)
add_custom_target("tests" DEPENDS examples ${overlay_root}
COMMAND find ${example_wrkdir} -name "tests.ke" | xargs -I{} rsync {} ${overlay_root}
COMMENT "Copying example enclave packages"
)
###############################################################################
## COMPONENT: image
###############################################################################
add_custom_target("image-deps" DEPENDS "tests" "driver" ${overlay_root}
COMMAND find ${driver_wrkdir} -name "*.ko" -exec cp {} ${overlay_root} \\\\;
)
add_custom_target("image" DEPENDS "buildroot" "sm"
COMMENT "Generating image"
)
add_dependencies("buildroot" "image-deps")
###############################################################################
## PATCH
###############################################################################
add_custom_target("patch" DEPENDS ${qemu_patches} ${linux_patches} ${sm_patches})
###############################################################################
## QEMU scripts
###############################################################################
set(scripts ${CMAKE_BINARY_DIR}/scripts)
add_custom_command(OUTPUT ${scripts} COMMAND mkdir -p ${scripts})
# if initramfs is false, we need to tell qemu where to find the block device
if(initramfs)
set(extra_qemu_options "")
else()
set(extra_qemu_options "\
-append \"console=ttyS0 ro root=/dev/vda\" \
-drive file=${buildroot_wrkdir}/images/rootfs.ext2,format=raw,id=hd0 \
-device virtio-blk-device,drive=hd0 \
")
endif()
# generate the qemu runscript, using the above options
add_custom_command(OUTPUT ${scripts}/run-qemu.sh DEPENDS ${scripts}
WORKING_DIRECTORY ${scripts}
COMMAND echo "\
export HOST_PORT=\${HOST_PORT:=\"\$((3000 + RANDOM % 3000))\"}; \
echo \"**** Running QEMU SSH on port \${HOST_PORT} ****\"; \
export SMP=1; \
while [ \"\$1\" != \"\" ]; do if [ \"\$1\" = \"-debug\" ]; then echo \"**** GDB port \$((HOST_PORT + 1)) ****\"; DEBUG=\"-gdb tcp::\$((HOST_PORT + 1)) -S -d in_asm -D debug.log\"; fi; if [ \"\$1\" = \"-smp\" ]; then SMP=\"\$2\"; shift; fi; shift; done; \
${qemu_system} \
\$DEBUG \
-m 2G \
-nographic \
-machine virt \
-bios ${bootrom_wrkdir}/bootrom.bin \
-kernel ${fw_elf} \
${extra_qemu_options} \
-netdev user,id=net0,net=192.168.100.1/24,dhcpstart=192.168.100.128,hostfwd=tcp::\$\{HOST_PORT\}-:22 \
-device virtio-net-device,netdev=net0 \
-device virtio-rng-pci \
-smp $SMP" > run-qemu.sh
VERBATIM
COMMAND
chmod +x run-qemu.sh
)
add_custom_command(OUTPUT ${scripts}/test-qemu.sh DEPENDS ${CMAKE_SOURCE_DIR}/scripts ${scripts}
COMMAND cp ${CMAKE_SOURCE_DIR}/scripts/test-qemu.sh ${scripts})
add_custom_command(OUTPUT ${scripts}/travis.sh DEPENDS ${CMAKE_SOURCE_DIR}/scripts ${scripts}
COMMAND cp ${CMAKE_SOURCE_DIR}/scripts/travis.sh ${scripts})
add_custom_command(OUTPUT ${scripts}/gdb.sh DEPENDS ${CMAKE_SOURCE_DIR}/scripts ${scripts}
COMMAND cp ${CMAKE_SOURCE_DIR}/scripts/gdb.sh ${scripts})
add_custom_target(
"tools" ALL
DEPENDS ${scripts} ${scripts}/run-qemu.sh ${scripts}/test-qemu.sh ${scripts}/travis.sh ${scripts}/gdb.sh
COMMENT "Generating scripts and tools"
)
add_custom_target(
"run-tests"
DEPENDS "tools" "image"
COMMAND
./scripts/travis.sh ${CMAKE_SOURCE_DIR}/tests
COMMENT "Running tests"
)