1
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include <sancus_support/tsc.h>
6 #include "ta_internals.h"
7
8
9 typedef struct TAProcess {
10 int is_used;
11 struct process *process;
12 void *thread;
13 void *thread_end;
14 unsigned long int invoked_n;
15 uint64_t invoked_t;
16 #ifdef __SANCUS_MSP430
17 unsigned char padding[12];
18 #endif
19 } TAProcess;
20
21 static TAProcess SM_DATA(TA_SM_NAME) TAProcesses[MAX_PROCESSES];
22
23
24 static void * SM_FUNC(TA_SM_NAME)
25 TAFindRet (void *start)
26 {
27 unsigned char *addr = start;
28
29
30 while (addr < (unsigned char *) 0xffff) {
31
32 if (addr[0] == 0x30 && addr[1] == 0x41) {
33 return (addr + 1);
34 }
35 addr++;
36 }
37
38 return (NULL);
39 }
40
41 static int SM_FUNC(TA_SM_NAME)
42 TAAddProcess (struct process *p)
43 {
44 int i;
45
46 __TIMING_INIT();
47
48
49 for (i = 0; i < MAX_PROCESSES; i++) {
50 if (TAProcesses[i].is_used != 0 &&
51 TAProcesses[i].process == p &&
52 TAProcesses[i].thread == p->thread) {
53 return (TA_SUCCESS);
54 }
55 }
56
57
58 for (i = 0; i < MAX_PROCESSES; i++) {
59 if (TAProcesses[i].is_used == 0) {
60 TAProcesses[i].is_used = 1;
61 TAProcesses[i].process = p;
62 TAProcesses[i].thread = p->thread;
63 TAProcesses[i].invoked_n = 0;
64 TAProcesses[i].invoked_t = 0;
65 if (TAProcesses[i].thread != NULL) {
66 TAProcesses[i].thread_end = TAFindRet(p->thread);
67 }
68 #ifdef __DEBUG
69 printf3 ("TA: adding process: %s @ %p..%p\n", (void *) p->name,
70 TAProcesses[i].thread, TAProcesses[i].thread_end);
71 #endif
72
73 __TIMING_PRINT("+++ TAAddProcess(): ");
74
75 if (TAProcesses[i].thread != NULL &&
76 TAProcesses[i].thread_end != NULL) {
77 TARegisterInvar (TAProcesses[i].thread,
78 (TAProcesses[i].thread_end - TAProcesses[i].thread));
79 }
80 return (TA_SUCCESS);
81 }
82 }
83
84 return (TA_FAILURE);
85 }
86
87 int SM_FUNC(TA_SM_NAME)
88 TAProcessUpdate (void *thread)
89 {
90 int i;
91 for (i = 0; i < MAX_PROCESSES; i++) {
92 if (TAProcesses[i].thread == thread) {
93 TAProcesses[i].invoked_n += 1;
94 TAProcesses[i].invoked_t = (uint64_t) tsc_read();
95 return (TA_SUCCESS);
96 }
97 }
98 return (TA_FAILURE);
99 }
100
101 int SM_FUNC(TA_SM_NAME)
102 TAScanProcessesInit (void)
103 {
104 struct process *p;
105
106 p = process_list;
107 while (p) {
108 #ifdef __DEBUG
109 printf2 ("TA: process found: %s @ %p\n", (void *) p->name, p->thread);
110 #endif
111 TAAddProcess (p);
112 p = p->next;
113 }
114
115 return (TA_SUCCESS);
116 }
117
118 int SM_FUNC(TA_SM_NAME)
119 TACheckProcesses (void)
120 {
121 int i, j, found;
122 struct process *p;
123
124 j = 0;
125 for (i = 0; i < MAX_PROCESSES; i++) {
126 if (TAProcesses[i].is_used != 0) {
127 j++;
128 found = 0;
129 p = process_list;
130 while (p) {
131 if (TAProcesses[i].process == p &&
132 TAProcesses[i].thread == p->thread) {
133 found = 1;
134 }
135 p = p->next;
136 }
137 if (! found) {
138 printf1 ("TA: *** process @ %p removed!\n",
139 TAProcesses[i].thread);
140 }
141 }
142 }
143
144 #ifdef __DEBUG
145 printf1 ("TA: %i processes checked.\n", (void *) j);
146 #endif
147 return (TA_SUCCESS);
148 }
149
150
151 int SM_FUNC(TA_SM_NAME)
152 TAProcInit (void)
153 {
154 int i;
155
156 for (i = 0; i < MAX_PROCESSES; i++) {
157 TAProcesses[i].is_used = 0;
158 }
159
160 return (TA_SUCCESS);
161 }
162
163
164 unsigned char* SM_ENTRY(TA_SM_NAME) TAProcessStatus (unsigned char *nonce,
165 int nonce_len, int* status_len)
166 {
167 static unsigned char *ProcessStatus = NULL;
168 static int sl;
169
170 __TIMING_INIT();
171
172 sl = (sizeof(struct TAProcess) * MAX_PROCESSES) + nonce_len;
173 *status_len = sl + HASH_SIZE;
174 ProcessStatus = malloc (*status_len);
175 if (!ProcessStatus) { goto FAIL; }
176
177 sancus_wrap(nonce, nonce_len, TAProcesses, *status_len, ProcessStatus,
178 ProcessStatus + sl);
179
180 __TIMING_PRINT("+++ TAProcessStatus(): ");
181
182 return (ProcessStatus);
183
184 FAIL:
185 *status_len = 0;
186 return (NULL);
187 }
188
189
190
191