Skip to content

Commit

Permalink
Try #1:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Feb 10, 2022
2 parents 2765856 + 36b4f8f commit cbb95c4
Show file tree
Hide file tree
Showing 15 changed files with 570 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .buildbot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

set -e

build_dir="$(pwd)/build"
src_dir="$(pwd)/"
cheri_dir="$cheri_dir"

export CC=$cheri_dir/morello-sdk/bin/clang
export CFLAGS="--config cheribsd-morello-hybrid.cfg"
export ASMFLAGS="--config cheribsd-morello-hybrid.cfg"

# Build project locally
cmake \
-G Ninja \
-DCMAKE_BUILD_TYPE=DEBUG \
-DLLVM_DIR=$cheri_dir/morello-sdk/lib/cmake/llvm \
-DClang_DIR=$cheri_dir/morello-sdk/lib/cmake/clang \
-B $build_dir \
-S $src_dir
cmake --build $build_dir

# Spawn CHERI QEMU instance
NETDEV="user,id=net0,hostfwd=tcp::10086-:22"
$cheri_dir/morello-sdk/bin/qemu-system-morello \
-M virt,gic-version=3 \
-cpu morello \
-m 2048 \
-nographic \
-bios edk2-aarch64-code.fd \
-drive if=none,file=$cheri_dir/cheribsd-morello-hybrid.img,id=drv,format=raw \
-device virtio-blk-device,drive=drv \
-device virtio-net-device,netdev=net0 \
-netdev $NETDEV \
-device virtio-rng-pci

# Execute tests via CMake
cd $build_dir
ctest
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
build/

**/*.swp

build.sh
up.sh
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.16)
project(CheriMorelloCompartments LANGUAGES C ASM)

set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
set(SRC_DIR ${CMAKE_SOURCE_DIR}/src)
set(TEST_DIR ${CMAKE_SOURCE_DIR}/tests)

# Setup
include_directories("${INCLUDE_DIR}")

find_package(Clang REQUIRED)
find_package(LLVM REQUIRED CONFIG)

# Building
add_subdirectory(${SRC_DIR})

# Testing
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)
endif()

if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
add_subdirectory(${TEST_DIR})
endif()
17 changes: 17 additions & 0 deletions COPYRIGHT
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Except as otherwise noted (below and/or in individual files), this project is
licensed under the Apache License, Version 2.0 <LICENSE-APACHE>
<http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT>
<http://opensource.org/licenses/MIT>, at your option.

Copyright is retained by contributors and/or the organisations they
represent(ed) -- this project does not require copyright assignment. Please see
version control history for a full list of contributors. Note that some files
may include explicit copyright and/or licensing notices.

The following contributors wish to explicitly make it known that the copyright
of their contributions is retained by an organisation:

Jacob Bramley <[email protected]>:
copyright retained by Arm Limited
Laurence Tratt <[email protected]>:
copyright retained by King's College London
10 changes: 10 additions & 0 deletions LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
17 changes: 17 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions bors.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Sourced from
# https://github.com/capablevms/cheri-examples/blob/master/bors.toml
status = ["buildbot/capablevms-test-script"]

timeout_sec = 600 # 10 minutes

# Have bors delete auto-merged branches
delete_merged_branches = true

cut_body_after = ""
6 changes: 6 additions & 0 deletions include/comps_offsets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#define COMP_SIZE 48
#define COMP_OFFSET_PCC 0
#define COMP_OFFSET_DDC 16
#define COMP_OFFSET_STK_ADDR 32

#define MAX_COMP_COUNT 2
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_library(morello-compartments STATIC
${SRC_DIR}/manager.S
${SRC_DIR}/switcher.S
)
169 changes: 169 additions & 0 deletions src/manager.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
.data

comps_cnt: .dword 0
comps_addr: .dword 0
switcher_caps: .dword 0

.text
.balign 4

#include "comps_offsets.h"

// Variables
.global comps_cnt
.global comps_addr
.global switcher_caps

// Functions
.global asm_call_wrapper
.global init_compartments
.global add_compartment
.global del_compartment

/**
* Wrapper to store/restore state when coming from C
*
* @param x0 ASM function to call
* @param x1-x6 parameters to pass to function in c0
*/
.type asm_call_wrapper, "function"
asm_call_wrapper:
// Save `x0` so we can temporarily use it
cvtp c0, x0
str c0, [sp]

// Derive `clr` (in case asm function does something weird with `PCC`)
cvtp c0, lr
swp c0, c0, [sp]
sub sp, sp, #16

blr c0
ldr clr, [sp, #16]!
ret clr

/**
* Sets up memory for compartments
*
* Initializes required memory for compartments. This involves allocating a
* memory region for use by switcher code, to contain required capabilities,
* and deriving appropriate PCC/DDC values containing the executable switcher
* code, and the aforementioned memory region, respectively.
*
* @return Pointer to newly allocated memory region
*/
.type init_compartments, "function"
init_compartments:
// Compute size of required memory, equivalent to `length` parameter of
// `mmap`
mov x0, #COMP_SIZE
mov x1, #MAX_COMP_COUNT
mov x2, #32 // size of the 2 switcher capabilities
madd x1, x0, x1, x2

// Store length and `lr` on stack, as we'll need them later
stp x1, lr, [sp, #-16]!

// Allocate memory for switcher
mov x0, xzr // address
// length - already stored in `x1`
mov w2, #3 // prot == PROT_READ | PROT_WRITE
mov w3, #4098 // flags == MAP_PRIVATE | MAP_ANONYMOUS
mov w4, #-1 // fd
mov w5, wzr // offset
bl mmap

// Restore length and `lr`
ldp x1, lr, [sp], #16

// Save pointer to new allocated memory in `switcher_caps`
adr x2, switcher_caps
str x0, [x2]

// Derive DDC for switcher
cvtp c2, x0
scbnds c2, c2, x1

// Derive PCC for `switch_compartments` and friends
adr x3, switcher_entry
adr x4, switch_compartment_end
sub x4, x4, x3
cvtp c3, x3
scbndse c3, c3, x4

// Store (DDC, PCC) at `switcher_caps`
ldr x1, switcher_caps
stp c2, c3, [x1], #32

// Save start address for compartment capabilities in `comps_addr`
adr x2, comps_addr
str x1, [x2]

ret

/**
* Function to add information for a compartment
*
* @param x0 Compartment memory size
* @param x1 Compartment executable function
*
* @return Pointer to newly allocated memory region
*/
.type add_compartment, "function"
add_compartment:
// Store inputs and `lr` so we can call `mmap`
stp x0, x1, [sp, #-32]!
str lr, [sp, #16]
mov x1, x0

// Allocate memory for new compartment
mov x0, xzr // address
// length - already stored in `x1`
mov w2, #3 // prot == PROT_READ | PROT_WRITE
mov w3, #4098 // flags == MAP_PRIVATE | MAP_ANONYMOUS
mov w4, #-1 // fd
mov w5, wzr // offset
bl mmap

// Restore memory size and function address
ldp x1, x2, [sp], #32
ldr lr, [sp, #-16]

// Derive compartment DDC
cvtp c0, x0
scbnds c0, c0, x1

// Derive compartment PCC
cvtp c1, x2
mov x2, #320 // TODO dynamic value
scbndse c1, c1, x2

// Store new PCC and DDC
ldr x2, comps_addr
ldr x3, comps_cnt
mov x4, #COMP_SIZE
madd x2, x3, x4, x2
stp c0, c1, [x2]

// Increment counter
adr x3, comps_cnt
ldr x4, [x3]
add x4, x4, #1
str x4, [x3]

// Update switcher DDC
//adr x2, switcher_caps
//ldr c0, [x2]
//gclen x1, c0
//add x1, x1, #32
//scbndse c0, c0, x1
//str c0, [x2]

ret

/**
* Function to delete an existing compartment data
*
* @param c0 ID of compartment to be deleted
*/
.type del_compartment, "function"
del_compartment:
Loading

0 comments on commit cbb95c4

Please sign in to comment.