-
Notifications
You must be signed in to change notification settings - Fork 482
/
Copy pathmemory-access.c
215 lines (192 loc) · 5.86 KB
/
memory-access.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Acess guest physical memory via a domain socket.
*
* Copyright (C) 2011 Sandia National Laboratories
* Author: Bryan D. Payne ([email protected])
*/
#include "memory-access.h"
//#include "cpu-all.h"
#include "qemu-common.h"
#include "cpu-common.h"
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <signal.h>
#include <stdint.h>
struct request{
uint8_t type; // 0 quit, 1 read, 2 write, ... rest reserved
uint64_t address; // address to read from OR write to
uint64_t length; // number of bytes to read OR write
};
static uint64_t
connection_read_memory (uint64_t user_paddr, void *buf, uint64_t user_len)
{
target_phys_addr_t paddr = (target_phys_addr_t) user_paddr;
target_phys_addr_t len = (target_phys_addr_t) user_len;
void *guestmem = cpu_physical_memory_map(paddr, &len, 0);
if (!guestmem){
return 0;
}
memcpy(buf, guestmem, len);
cpu_physical_memory_unmap(guestmem, len, 0, len);
return len;
}
static uint64_t
connection_write_memory (uint64_t user_paddr, void *buf, uint64_t user_len)
{
target_phys_addr_t paddr = (target_phys_addr_t) user_paddr;
target_phys_addr_t len = (target_phys_addr_t) user_len;
void *guestmem = cpu_physical_memory_map(paddr, &len, 1);
if (!guestmem){
return 0;
}
memcpy(guestmem, buf, len);
cpu_physical_memory_unmap(guestmem, len, 0, len);
return len;
}
static void
send_success_ack (int connection_fd)
{
uint8_t success = 1;
int nbytes = write(connection_fd, &success, 1);
if (1 != nbytes){
printf("QemuMemoryAccess: failed to send success ack\n");
}
}
static void
send_fail_ack (int connection_fd)
{
uint8_t fail = 0;
int nbytes = write(connection_fd, &fail, 1);
if (1 != nbytes){
printf("QemuMemoryAccess: failed to send fail ack\n");
}
}
static void
connection_handler (int connection_fd)
{
int nbytes;
struct request req;
while (1){
// client request should match the struct request format
nbytes = read(connection_fd, &req, sizeof(struct request));
if (nbytes != sizeof(struct request)){
// error
continue;
}
else if (req.type == 0){
// request to quit, goodbye
break;
}
else if (req.type == 1){
// request to read
char *buf = malloc(req.length + 1);
nbytes = connection_read_memory(req.address, buf, req.length);
if (nbytes != req.length){
// read failure, return failure message
buf[req.length] = 0; // set last byte to 0 for failure
nbytes = write(connection_fd, buf, req.length + 1);
}
else{
// read success, return bytes
buf[req.length] = 1; // set last byte to 1 for success
nbytes = write(connection_fd, buf, req.length + 1);
}
free(buf);
}
else if (req.type == 2){
// request to write
void *write_buf = malloc(req.length);
nbytes = read(connection_fd, write_buf, req.length);
if (nbytes != req.length){
// failed reading the message to write
send_fail_ack(connection_fd);
}
else{
// do the write
nbytes = connection_write_memory(req.address, write_buf, req.length);
if (nbytes == req.length){
send_success_ack(connection_fd);
}
else{
send_fail_ack(connection_fd);
}
}
free(write_buf);
}
else{
// unknown command
printf("QemuMemoryAccess: ignoring unknown command (%d)\n", req.type);
char *buf = malloc(1);
buf[0] = 0;
nbytes = write(connection_fd, buf, 1);
free(buf);
}
}
close(connection_fd);
}
static void *
connection_handler_gate (void *fd)
{
connection_handler(*(int *)fd);
printf("QemuMemoryAccess: Connection done (%d)\n", *(int *)fd);
free(fd);
return NULL;
}
static void *
memory_access_thread (void *path)
{
struct sockaddr_un address;
int socket_fd, connection_fd, *tmp_fd;
pthread_t thread;
socklen_t address_length;
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (socket_fd < 0){
printf("QemuMemoryAccess: socket failed\n");
goto error_exit;
}
unlink(path);
address.sun_family = AF_UNIX;
address_length = sizeof(address.sun_family) + sprintf(address.sun_path, "%s", (char *) path);
if (bind(socket_fd, (struct sockaddr *) &address, address_length) != 0){
printf("QemuMemoryAccess: bind failed\n");
goto error_exit;
}
if (listen(socket_fd, 0) != 0){
printf("QemuMemoryAccess: listen failed\n");
goto error_exit;
}
while (true) {
connection_fd = accept(socket_fd, (struct sockaddr *) &address, &address_length);
printf("QemuMemoryAccess: Connction accepted on %d.\n", connection_fd);
tmp_fd = (int *) calloc(1, sizeof(int));
*tmp_fd = connection_fd;
pthread_create(&thread, NULL, connection_handler_gate, tmp_fd);
}
close(socket_fd);
unlink(path);
error_exit:
return NULL;
}
int
memory_access_start (const char *path)
{
pthread_t thread;
sigset_t set, oldset;
int ret;
// create a copy of path that we can safely use
char *pathcopy = malloc(strlen(path) + 1);
memcpy(pathcopy, path, strlen(path) + 1);
// start the thread
sigfillset(&set);
pthread_sigmask(SIG_SETMASK, &set, &oldset);
ret = pthread_create(&thread, NULL, memory_access_thread, pathcopy);
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
return ret;
}