-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyscall.cc
More file actions
157 lines (144 loc) · 3.92 KB
/
Copy pathsyscall.cc
File metadata and controls
157 lines (144 loc) · 3.92 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdlib.h>
#include "syscall.h"
#include "memory_monitor.h"
#include "alias.h"
#include "register_file.h"
#include "machine.h"
#include <string.h>
//need include h file of load and store
void sys_exit(Machine *machine){
printf("the program has exited successfully\n");
machine->Evaluate();
machine->memory->PrintHitTime();
exit(0);
}
/*
void sys_write(lint reg10, lint reg11, lint reg12, Memory *memory){
printf("sys_write\n");
unsigned long long int cur_addr = reg11;
unsigned long long int f_addr = reg12+reg11;
char* s = (char*)memory->Load(cur_addr, reg12);
for(unsigned long long int i = 0; i < reg12; i++)
{
unsigned char a = s[i];
printf("%c", a);
}
}*/
void sys_write(lint reg10, lint reg11, lint reg12, MemoryMonitor *memory, RegisterFile *file){
//printf("sys_write, position: %llx\n", reg11);
// we don't have fd
if (reg10 != 1){
printf("error!!! not stdout!!!\n");
exit(0);
}
else{
// need load function from Jingyue Gao
//void* location = memory->Translate(reg11);
//write((int)reg10, location, 1);
void* location = memory->Load(reg11, reg12);
int writeNum = write((int)reg10, (char *)location, (int)reg12);
file->setInteger(A0, writeNum);
}
}
void sys_read(lint reg10, lint reg11, lint reg12, MemoryMonitor *memory, RegisterFile *file)
{
//printf("sys_read, position: %llx\n", reg11);
// we don't have fd
if (reg10 != 0)
{
printf("error!!! not stdin!!!\n");
exit(0);
}
else{
char* readposition = (char*)malloc((int)reg12 + 5);
int readNum = read((int)reg10, readposition, (int)reg12);
//int readNum = read("%s", readposition);
//printf("readposition: %s\n", readposition);
//read((int)reg10, readposition, (int)reg12);
// need store function from Jingyue Gao
memory->Store(reg11, strlen(readposition) + 1, (char*)readposition);
//void* location = memory->Load(reg11, (int)reg12);
//printf("saved %s\n", location);
delete readposition;
file->setInteger(A0, readNum);
}
}
void sys_gettimeofday(lint reg10, MemoryMonitor *memory){
//printf("sys_gettimeofday\n");
struct timeval t;
if (gettimeofday(&t, NULL) == 0){
memory->Store(reg10, sizeof(t), (char*)&t);
}
else{
printf("gettimeofday error\n");
exit(0);
}
}
/*
void sys_fstat(lint reg10, lint reg11, Memory *memory, RegisterFile *file){
//printf("sys_fstat\n");
void* location = memory->Translate(reg11);
int num = fstat(reg10, (struct stat *)location);
file->setInteger(A0, num);
}
void sys_close(lint reg10, Memory *memory, RegisterFile *file){
//printf("sys_close\n");
int num = close(reg10);
file->setInteger(A0, num);
}
void sys_brk(lint reg10, Memory *memory, RegisterFile *file){
//printf("sys_brk\n");
void* location = memory->Translate(reg10);
int num = brk((void*)location);
file->setInteger(A0, num);
}
void sys_lseek(lint reg10, lint reg11, lint reg12, Memory *memory,RegisterFile *file){
//printf("sys_lseek\n");
off_t num = lseek(reg10, reg11, reg12);
if (reg10 != 0)
{
printf("error!!! not stdin!!!\n");
exit(0);
}
file->setInteger(A0, num);
}*/
void syscall(lint reg10, lint reg11, lint reg12, lint reg13, lint reg17, MemoryMonitor *memory, RegisterFile *file, Machine *machine)
{
switch(reg17){
case SYS_exit:
sys_exit(machine);
break;
case SYS_read:
sys_read(reg10, reg11, reg12, memory, file);
break;
case SYS_write:
sys_write(reg10, reg11, reg12, memory, file);
break;
case SYS_gettimeofday:
sys_gettimeofday(reg10, memory);
break;
case SYS_close:
//sys_close(reg10, memory, file);
file->setInteger(A0, 0);
break;
case SYS_fstat:
//sys_fstat(reg10, reg11, memory, file);
file->setInteger(A0, 0);
break;
case SYS_brk:
//sys_brk(reg10, memory, file);
file->setInteger(A0, 0);
break;
case SYS_lseek:
//sys_lseek(reg10, reg11, reg12, memory, file);
file->setInteger(A0, 0);
break;
default:
printf("%d syscall hasn't been defined\n", reg17);
}
}