Newer
Older
Yuxiao Mao
committed
#include <stddef.h>
#include "myorca_uart.h"
#define __OPTION_ATTACK_ENABLE__ 1
#define __OPTION_VICTIM_ENABLE__ 1
// Attack and Victim code
#if __OPTION_ATTACK_ENABLE__ == 1
#include "attack.h"
#endif
#if __OPTION_VICTIM_ENABLE__ == 1
#include "victim.h"
#endif
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
Yuxiao Mao
committed
int a2d(char ch);
void victim_1(int32_t* val_addr);
// ----------------------------------------------------------------------------
Yuxiao Mao
committed
// Place order seems to be the same :
// static before non static, used array before unused
// heap at the end if non static, preserve order if all static
// ----------------------------------------------------------------------------
Yuxiao Mao
committed
unsigned char myorca_heap[HEAP_SIZE_BYTE];
// Cache orca : tag (23b), num_line (4b = 16line), pos (5b = 32byte)
// To cover all cache lines = 16lines * 4byte/int * 8int
// But if we monitor all cache lines, attack_fr structure and heap likely occupy
// all address and thought can't detect any eviction.
// In this configuration of 8 lines, we can detect 5-7 access.
// MONITOR_CACHE_LINE must < 16, else need change VLIST_DEF_SIZE, victim
// address selection, attack line detect print logic.
#define MONITOR_CACHE_LINE 8
#define INT_PER_CACHE_LINE 8
static int32_t victim_data_address[16*INT_PER_CACHE_LINE];
// ----------------------------------------------------------------------------
// Main
// ----------------------------------------------------------------------------
// Prepare heap
orca_init_malloc(myorca_heap, HEAP_SIZE_BYTE, 4);
Yuxiao Mao
committed
uart_printf("\r\n\r\nHEAP_INIT");
#if __OPTION_ATTACK_ENABLE__ == 1
Yuxiao Mao
committed
uint16_t res[MONITOR_CACHE_LINE];
attack_prepare(victim_data_address);
Yuxiao Mao
committed
for (int i = 1; i < MONITOR_CACHE_LINE; i++) {
attack_add_monitor(&victim_data_address[i*INT_PER_CACHE_LINE]);
}
uart_printf("\r\nATTACK_ENABLE");
#if __OPTION_VICTIM_ENABLE__ == 1
unsigned int uart_read_fifo_count = 0;
while (1) {
wait(1000);
uart_read_fifo_count = uart_get_read_fifo_count();
if (uart_read_fifo_count > 0) {
char read_value = uart_read_char();
uart_printf("\r\nReceived value from Uart: (");
uart_printc(read_value);
uart_printf(").");
#if __OPTION_ATTACK_ENABLE__ == 1
Yuxiao Mao
committed
int read_number = a2d(read_value);
if (read_number >= 0 && read_number < MONITOR_CACHE_LINE) {
victim_1(&victim_data_address[read_number*INT_PER_CACHE_LINE]);
} else {
victim_1(victim_data_address);
}
Yuxiao Mao
committed
for (int i = 0; i < MONITOR_CACHE_LINE; i++) {
if (res[i] < 3) {
uart_printc('a');
uart_printc(i+(i<10 ? '0' : 'A'-10));
}
#else
}
}
return 0;
}
// ----------------------------------------------------------------------------
// Exception handling
// ----------------------------------------------------------------------------
uart_printf("Exception\r\n");
while (1) {
}
Yuxiao Mao
committed
// ----------------------------------------------------------------------------
// Others functions
// ----------------------------------------------------------------------------
int a2d(char ch)
{
if (ch>='0' && ch<='9')
return ch-'0';
else if (ch>='a' && ch<='f')
return ch-'a'+10;
else if (ch>='A' && ch<='F')
return ch-'A'+10;
else return -1;
}
// ----------------------------------------------------------------------------
// Victim code
// victim_1 provide simple memory access
// ----------------------------------------------------------------------------
Yuxiao Mao
committed
void victim_1(int32_t* val_addr) {
Yuxiao Mao
committed
uart_printi(*val_addr); //take int
*val_addr = *val_addr + 1;