-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbar.c
48 lines (37 loc) · 1.05 KB
/
bar.c
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
#include "bar.h"
#include <sancus_support/sm_io.h>
DECLARE_SM(bar, 0x1234);
typedef struct {
int a;
int b;
int c;
} element_t;
#define BAR_TABLE_SIZE 3
/* The following data should be stored in the immutable text section. */
const element_t SM_DATA(bar) bar_table[BAR_TABLE_SIZE] = {
{1, 2, 3}, {4, 5, 6}, {7, 8, 9}
};
int SM_FUNC(bar) bar_ispow2( int i )
{
while (((i % 2) == 0) && i > 1) i /= 2;
return (i == 1);
}
int SM_FUNC(bar) bar_in_text( void *p )
{
return (p >= (void*) &__PS(bar)) && (p < (void*) &__PE(bar));
}
void SM_FUNC(bar) bar_assert(void)
{
ASSERT(sancus_get_caller_id() == 1);
ASSERT(sancus_get_self_id() == 2);
ASSERT( !bar_ispow2(sizeof(element_t)) );
ASSERT( bar_in_text((void*) bar_table ));
ASSERT( bar_in_text((void*) bar_table + BAR_TABLE_SIZE - 1) );
}
int SM_ENTRY(bar) bar_lookup( int idx )
{
bar_assert();
/* Array indexing below generates muliplication library call that should be
intercepted and inlined by sancus-ld. */
return bar_table[idx % BAR_TABLE_SIZE].a;
}