-
Notifications
You must be signed in to change notification settings - Fork 35
/
camkes_vm_helpers.cmake
350 lines (323 loc) · 13 KB
/
camkes_vm_helpers.cmake
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
#
# Copyright 2018, Data61, CSIRO (ABN 41 687 119 230)
#
# SPDX-License-Identifier: BSD-2-Clause
#
cmake_minimum_required(VERSION 3.8.2)
set(VM_PROJECT_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "")
# Function for declaring a CAmkESVM. This is called for each Init component in the applications
# camkes config.
# init_component: is the name of CamkESVM Init component described in the .camkes config
# In addition the user can pass in extra compilation sources, includes, libs and flags through
# the SOURCES, INCLUDES, LIBS, LD_FLAGS and C_FLAGS arguments.
function(DeclareCAmkESVM init_component)
cmake_parse_arguments(
PARSE_ARGV
1
VM_COMP
""
""
"EXTRA_SOURCES;EXTRA_INCLUDES;EXTRA_LIBS;EXTRA_C_FLAGS;EXTRA_LD_FLAGS"
)
# Retrieve sources for Init component
file(GLOB base_sources ${VM_PROJECT_DIR}/components/Init/src/*.c)
# Declare an Init component for CAmkESVM
DeclareCAmkESComponent(
${init_component}
SOURCES
${base_sources}
${VM_COMP_EXTRA_SOURCES}
INCLUDES
${VM_PROJECT_DIR}/components/Init/src
${VM_PROJECT_DIR}/components/Init/include
${VM_PROJECT_DIR}/components/VM/configurations
${VM_COMP_EXTRA_INCLUDES}
LIBS
sel4allocman
sel4vm
sel4vmmplatsupport
sel4vmmplatsupport_Config
sel4_autoconf
camkes_vmm_Config
virtqueue
vswitch
FileServer-client
${VM_COMP_EXTRA_LIBS}
LD_FLAGS
${VM_COMP_EXTRA_LD_FLAGS}
C_FLAGS
${VM_COMP_EXTRA_C_FLAGS}
TEMPLATE_SOURCES
seL4ExtraRAM.template.c
seL4ExcludeGuestPAddr.template.c
seL4InitConnection.template.c
seL4VMIOPorts.template.c
seL4GuestMaps.template.c
seL4VMIRQs.template.c
seL4VMPCIDevices.template.c
)
endfunction(DeclareCAmkESVM)
#
# Function defines a CAmkESVMFileServer using the declared fileserver images
# and fileserver dependencies. These images are placed into a CPIO archive.
#
# Parameters:
#
# TYPE <type>
# Type of the file server CAmkES component.
# Optional, defaults to "FileServer"
#
# INSTANCE <name>
# Instance name of the file server CAmkES component.
# Optional, defaults to "fserv"
#
# FILES <item>[ <item>[...]]
# The files to be added. Each item has the form [<NAME>:]<FILE_NAME>, where
# the optional <NAME> allows using a different name for the file in the
# file server than it had on the disk. The build will abort if <FILE_NAME> is
# not found. Each item can either be a single file item or a CMake list of
# items (where a CMake list is just a string with elements separated by ';').
# This allows building lists of files in advance, which may contain different
# files for different configurations. An empty string as item is also allowed
# explicitly for convenience reasons. This supports cases where an item does
# not exist in every configuration and the respective CMake variable used for
# the item is just left empty.
#
# DEPENDS <dep>[ <dep>[...]]
# Additional dependencies of the file server instance. This is an optional
# parameter for non-trivial dependencies. Each file server instance depends on
# all input files anyway, thus a re-build happens automatically on any
# changes. If an input file is created dynamically by another regular CMake
# target, any dependencies should have been specified there already, so there
# is no need to repeat them here.
#
#
function(DefineCAmkESVMFileServer)
cmake_parse_arguments(
PARSE_ARGV
0
PARAM # variable prefix
"" # option arguments
"TYPE;INSTANCE" # optional single value arguments
"FILES;DEPENDS" # optional multi value arguments
)
if(PARAM_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown arguments: ${PARAM_UNPARSED_ARGUMENTS}")
endif()
if(NOT PARAM_TYPE)
set(PARAM_TYPE "FileServer")
endif()
if(NOT PARAM_INSTANCE)
set(PARAM_INSTANCE "fserv")
endif()
# The target might exist already when AddToFileServer() was called.
set(FSRV_TARGET "vm_fileserver_config_${PARAM_INSTANCE}")
if(NOT TARGET ${FSRV_TARGET})
add_custom_target(${FSRV_TARGET})
endif()
# For dependencies and files, both lists and lists of list are supported for
# convenience reasons. Furthermore, empty entries are also allowed. This
# can happen when the caller uses variables for the lists, when in some
# configurations the lists remain empty.
foreach(element IN LISTS PARAM_DEPENDS)
foreach(item IN LISTS element)
if(item)
set_property(TARGET ${FSRV_TARGET} APPEND PROPERTY DEPS ${item})
endif()
endforeach()
endforeach()
foreach(element IN LISTS PARAM_FILES)
foreach(item IN LISTS element) # [<CPIO_NAME>:]<FILENAME>
if(item)
string(
REGEX
MATCH
"^([^:]+)(:([^:]+))?$"
cpio_item
"${item}"
)
if(NOT cpio_item)
message(FATAL_ERROR "invalid parameter format: '${item}'")
endif()
if(CMAKE_MATCH_3)
set(CPIO_NAME "${CMAKE_MATCH_1}")
set(FILE_NAME "${CMAKE_MATCH_3}")
else()
set(FILE_NAME "${CMAKE_MATCH_1}")
get_filename_component(CPIO_NAME "${FILE_NAME}" NAME)
endif()
set_property(
TARGET ${FSRV_TARGET}
APPEND
PROPERTY FILES "${CPIO_NAME}:${FILE_NAME}"
)
endif()
endforeach()
endforeach()
# now process the file/deps list
get_target_property(files ${FSRV_TARGET} FILES)
if(NOT files) # this also catches "files-NOTFOUND" if property is not set
set(files "")
endif()
get_target_property(deps ${FSRV_TARGET} DEPS)
if(NOT deps) # this also catches "deps-NOTFOUND" if property is not set
set(deps "")
endif()
set(CPIO_FILES "")
foreach(item IN LISTS files) # <CPIO_NAME>:<FILENAME>
string(
REGEX
MATCH
"^([^:]+):([^:]+)$"
cpio_item
"${item}"
)
if(NOT cpio_item)
message(FATAL_ERROR "invalid CPIO file format: '${item}'")
endif()
set(CPIO_NAME "${CMAKE_MATCH_1}")
set(FILE_NAME "${CMAKE_MATCH_2}")
set(CPIO_FILE "${PARAM_INSTANCE}/files/${CPIO_NAME}")
add_custom_command(
OUTPUT "${CPIO_FILE}"
COMMENT "copy: ${FILE_NAME} -> ${CPIO_FILE}"
COMMAND
${CMAKE_COMMAND} -E copy "${FILE_NAME}" "${CPIO_FILE}"
VERBATIM
DEPENDS ${FILE_NAME} ${deps}
)
# There is no need to create an explicit target for the command above,
# the archive creation depends on all files in CPIO_FILES, where the
# command above is the creation rule for each one.
list(APPEND CPIO_FILES "${CPIO_FILE}")
endforeach()
# Build CPIO archive. It implicitly depends on all files in CPIO_FILES,
# which have their own dependencies each from above. So we don't have any
# additional explicit dependencies here.
# Unfortunately MakeCPIO() currently allows passing plain file names only,
# it does not support paths. Thus, the archive will be created in the built
# output root folder, having it the instance specific subfolder would be a
# bit cleaner actually.
set(CPIO_ARCHIVE "${PARAM_INSTANCE}_cpio_archive.o")
include(cpio)
# Due to the way MakeCPIO() is implemented, the file list must have absolute
# paths. Since we don't require CMake 3.12+ yet, we can't use the list
# transformation functions, but have to prepend each element manually.
set(CPIO_FILES_FQN "")
foreach(f IN LISTS CPIO_FILES)
list(APPEND CPIO_FILES_FQN "${CMAKE_CURRENT_BINARY_DIR}/${f}")
endforeach()
MakeCPIO("${CPIO_ARCHIVE}" "${CPIO_FILES_FQN}")
# Build a library from the CPIO archive. Ensure the lib has a unique name
# within the project, as there could be more than one file server.
set(FILESERVER_LIB "${PARAM_INSTANCE}_file_archive_cpio")
add_library("${FILESERVER_LIB}" STATIC EXCLUDE_FROM_ALL "${CPIO_ARCHIVE}")
set_property(TARGET "${FILESERVER_LIB}" PROPERTY LINKER_LANGUAGE C)
# Add the CPIO-library to the FileServer component
ExtendCAmkESComponentInstance("${PARAM_TYPE}" "${PARAM_INSTANCE}" LIBS "${FILESERVER_LIB}")
endfunction(DefineCAmkESVMFileServer)
# Function for declaring the CAmkESVM root server. Taking the camkes application
# config file we declare a CAmkES Root server and the VM File Server. It is
# expected the caller has declared the file server images before using this
# function.
# camkes_config: The applications .camkes file
# In addition the user can pass in extra CPP compilation includes and flags through
# the CPP_INCLUDES and CPP_FLAGS arguments.
function(DeclareCAmkESVMRootServer camkes_config)
cmake_parse_arguments(PARSE_ARGV 1 CAMKES_ROOT_VM "" "" "CPP_INCLUDES;CPP_FLAGS")
# Initialise the CAmKES VM fileserver
DefineCAmkESVMFileServer()
get_absolute_source_or_binary(config_file "${camkes_config}")
# Declare CAmkES root server
DeclareCAmkESRootserver(
${config_file}
CPP_FLAGS
${CAMKES_ROOT_VM_CPP_FLAGS}
CPP_INCLUDES
"${VM_PROJECT_DIR}/components/VM"
${CAMKES_ROOT_VM_CPP_INCLUDES}
)
endfunction(DeclareCAmkESVMRootServer)
# Function for adding a file/image to the vm file server.
#
# Parameters:
#
# <filename_pref>
# The name to use for the file in the file server. Components using a file
# server could expect certain files to have a specific name, which could
# differer from the file name on the disk, so this provides a convenient way
# to handle the renaming.
#
# <file_dest>
# The location of the file on the disk.
#
# INSTANCE <name>
# The File server instance to add the file to.
# Optional, defaults to "fserv".
#
# DEPENDS <dep>[ <dep>[...]]
# Additional dependencies of the file added to the file server. This is an
# optional parameter for non-trivial dependencies of the input file. Each file
# server instance depends on all input files anyway, thus a re-build happens
# automatically on any changes. If an input file is created dynamically by
# another regular CMake target, any dependencies should have been specified
# there already, so there is no need to repeat them here.
#
function(AddToFileServer filename_pref file_dest)
cmake_parse_arguments(
PARSE_ARGV
2
PARAM # variable prefix
"" # option arguments
"INSTANCE" # optional single value arguments
"DEPENDS" # optional multi value arguments
)
if(PARAM_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown arguments: ${PARAM_UNPARSED_ARGUMENTS}")
endif()
if(NOT PARAM_INSTANCE)
set(PARAM_INSTANCE "fserv")
endif()
set(FSRV_TARGET "vm_fileserver_config_${PARAM_INSTANCE}")
if(NOT TARGET ${FSRV_TARGET})
add_custom_target(${FSRV_TARGET})
endif()
set_property(TARGET ${FSRV_TARGET} APPEND PROPERTY FILES "${filename_pref}:${file_dest}")
if(PARAM_DEPENDS)
set_property(TARGET ${FSRV_TARGET} APPEND PROPERTY DEPS ${PARAM_DEPENDS})
endif()
endfunction(AddToFileServer)
# Function for decompressing/extracting a vmlinux file from a given kernel image
# decompress_target: The target name the caller wishes to use to generate the decompressed kernel
# image
# decompressed_kernel_image: caller variable which is set with the decompressed kernel image location
# compressed_kernel_image: The location of the compressed kernel image
# DEPENDS: Any additional dependencies for the compressed kernel image
function(DecompressLinuxKernel decompress_target decompressed_kernel_image compressed_kernel_image)
# Get any existing dependencies for decompressing linux kernel
cmake_parse_arguments(PARSE_ARGV 3 DECOMPRESS_KERNEL "" "" "DEPENDS")
if(NOT "${DECOMPRESS_KERNEL_UNPARSED_ARGUMENTS}" STREQUAL "")
message(FATAL_ERROR "Unknown arguments to DecompressLinuxKernel")
endif()
# Retrieve filename from kernel path
get_filename_component(kernel_basename ${compressed_kernel_image} NAME)
# Extract vmlinux from bzimage
add_custom_command(
OUTPUT decomp/${kernel_basename}
COMMAND
bash -c
"${VM_PROJECT_DIR}/tools/elf/extract-vmlinux ${compressed_kernel_image} > decomp/${kernel_basename}"
VERBATIM
DEPENDS ${compressed_kernel_image} ${DECOMPRESS_KERNEL_DEPENDS}
)
# Create custom target for extraction
add_custom_target(
${decompress_target}
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/decomp/${kernel_basename}"
)
# Set parameter to tell the caller location of decompressed kernel image
set(
${decompressed_kernel_image} ${CMAKE_CURRENT_BINARY_DIR}/decomp/${kernel_basename}
PARENT_SCOPE
)
endfunction(DecompressLinuxKernel)