Skip to content

Commit

Permalink
Simplify test code and correct errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyma98 committed Aug 12, 2024
1 parent 8bd734f commit 25c2686
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 216 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/actions/build/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ runs:

# *** Tests for task - segmented stack ***

- name: Build test test-task-segmented_stack-verify_regs_stack_new_function
- name: Build test test-task-segmented_stack-function_arguments
uses: ./.github/workflows/actions/build-test
with:
category: task
sub-category: segmented_stack
test-name: verify_regs_stack_new_function
test-name: function_aruments

- name: Build test test-task-segmented_stack-nested_functions
uses: ./.github/workflows/actions/build-test
Expand Down
20 changes: 3 additions & 17 deletions .github/workflows/segmented_stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,19 @@ env:
CARGO_TERM_COLOR: always

jobs:
verify_regs_stack_new_function:
function_arguments:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Run test verify_regs_stack_new_function
- name: Run test function_arguments
uses: ./.github/workflows/actions/run-test
with:
cookie: ${{ secrets.cookie }}
category: task
sub-category: segmented_stack
test-name: verify_regs_stack_new_function

nested_functions:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Run test nested_functions
uses: ./.github/workflows/actions/run-test
with:
cookie: ${{ secrets.cookie }}
category: task
sub-category: segmented_stack
test-name: nested_functions
test-name: function_arguments

return_values:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ name = "test-task-unwind-deferred_nested_drop"
path = "examples/tests/task/unwind/deferred_nested_drop.rs"

[[example]]
name = "test-task-segmented_stack-verify_regs_stack_new_function"
path = "examples/tests/task/segmented_stack/verify_regs_stack_new_function.rs"
name = "test-task-segmented_stack-function_arguments"
path = "examples/tests/task/segmented_stack/function_arguments.rs"

[[example]]
name = "test-task-segmented_stack-nested_functions"
Expand Down
133 changes: 133 additions & 0 deletions examples/tests/task/segmented_stack/function_arguments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//! Test function arguments passing via both registers and the stack when a new
//! stacklet is allocated for a function call.
#![no_std]
#![no_main]
#![feature(naked_functions)]

extern crate alloc;
use core::arch::asm;
use hopter::{boot::main, debug::semihosting, hprintln, task};

#[naked]
extern "C" fn prepare_regs_stack() {
unsafe {
asm!(
// Segmented stack prologue.
// Stacklet allocation size 36, 20 for preserved values plus 16 for
// stack arguments.
// Stack argument size 0.
"mov.w r12, #0x20000000",
"ldr.w r12, [r12]",
"subs.w r12, sp, r12",
"cmp.w r12, #36",
"bge.n 0f",
"svc #255",
".short 9",
".short 0",

// Preserve callee-saved registers and return address.
"0:",
"push {{r4-r7, lr}}",

// Prepare arguments in registers r0-r3.
"mov r0, #1",
"mov r1, #2",
"mov r2, #3",
"mov r3, #4",

// Prepare arguments on stack.
"mov r4, #5",
"mov r5, #6",
"mov r6, #7",
"mov r7, #8",
"push {{r4-r7}}",

// Call `verify_arguments` function.
"bl {verify_arguments}",

// Discard stack arguments.
"add sp, #16",

// Restore callee-saved registers and return.
"pop {{r4-r7, pc}}",

verify_arguments = sym verify_arguments,
options(noreturn)
)
}
}

#[naked]
extern "C" fn verify_arguments() {
unsafe {
asm!(
// Segmented stack prologue. Request a huge stack frame of size
// 16384 bytes, which will very likely cause a new stacklet
// allocation.
//
// Stacklet allocation size 16384.
// Stack argument size 16.
"mov.w r12, #0x20000000",
"ldr.w r12, [r12]",
"subs.w r12, sp, r12",
"cmp.w r12, #16384",
"bge.n 0f",
"svc #255",
".short 4096",
".short 4",

"0:",
// Verify register arguments.
"cmp r0, #1",
"bne {error}",
"cmp r1, #2",
"bne {error}",
"cmp r2, #3",
"bne {error}",
"cmp r3, #4",
"bne {error}",

// Verify stack arguments.
"ldr r0, [sp, #0]",
"cmp r0, #5",
"bne {error}",
"ldr r0, [sp, #4]",
"cmp r0, #6",
"bne {error}",
"ldr r0, [sp, #8]",
"cmp r0, #7",
"bne {error}",
"ldr r0, [sp, #12]",
"cmp r0, #8",
"bne {error}",

// Print success message.
// This also tests tail call optimization.
"b {success}",

error = sym error,
success = sym success,
options(noreturn)
)
}
}

extern "C" fn success() {
hprintln!("Test Passed");
}

extern "C" fn error() {
hprintln!("Test Failed");
semihosting::terminate(false);
}

#[main]
fn main(_: cortex_m::Peripherals) {
task::build()
.set_entry(|| prepare_regs_stack())
.spawn()
.unwrap();
task::change_current_priority(10).unwrap();
semihosting::terminate(true);
}
32 changes: 15 additions & 17 deletions examples/tests/task/segmented_stack/nested_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

extern crate alloc;
use core::arch::asm;
use hopter::{boot::main, debug::semihosting, hprintln, schedule};

use hopter::{boot::main, debug::semihosting, hprintln, task};

#[naked]
extern "C" fn nested_function_calls() {
Expand All @@ -21,9 +20,9 @@ extern "C" fn nested_function_calls() {
".short 4", /* function_arg_size (right shifted by 2) ; <- PC + 2 */

"1:",
"push {{lr}}",
"mov r0, #0",
"push {{r0}}",
"push {{lr}}",
"mov r0, #0",
"push {{r0}}",
"bl {inner_function1}",
"pop {{r0, lr}}",
"bx lr",
Expand Down Expand Up @@ -99,34 +98,31 @@ extern "C" fn verify_arguments() {
"svc #255", /* otherwise, invoke SVC */
".short 250", /* (right shifted by 2) <- preserved PC */
".short 4", /* (right shifted by 2)" ; <- PC + 2 */
"1:",

"1:",
"push {{r4-r5, lr}}",

/* verify arguments on stack */
"ldr r3, [sp, #12]", /* Check last pushed value (r3). Load the value into r3 */
"cmp r3, #2",
"cmp r3, #2",
"bne {error}",

"ldr r4, [sp, #20]", /* Check second last pushed value (r2). Load the value into r4 */
"cmp r4, #1",
"cmp r4, #1",
"bne {error}",

"ldr r5, [sp, #28]", /*Check third last pushed value (r1). Load the value into r5 */
"cmp r5, #0",
"cmp r5, #0",
"bne {error}",


"bl {print_success}",
"pop {{r4-r5,lr}}",
"bx lr",

error = sym error,
print_success = sym success,
options(noreturn)

)

}
}

Expand All @@ -136,13 +132,15 @@ extern "C" fn success() {

extern "C" fn error() {
hprintln!("Test Failed");
semihosting::terminate(false);
}



#[main]
fn main(_: cortex_m::Peripherals) {
schedule::start_task(2, |_| nested_function_calls(), (), 0, 4).unwrap();
schedule::change_current_task_priority(10).unwrap();
task::build()
.set_entry(|| nested_function_calls())
.spawn()
.unwrap();
task::change_current_priority(10).unwrap();
semihosting::terminate(true);
}
Loading

0 comments on commit 25c2686

Please sign in to comment.