-
Notifications
You must be signed in to change notification settings - Fork 0
/
memutil.cpp
287 lines (247 loc) · 7.21 KB
/
memutil.cpp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
*
* Copyright (C) 1998-2020 Maruthi Seshidhar Inukonda - All Rights Reserved.
*
* This file is released under the LGPL v2.
*
* This file may be redistributed under the terms of the Lesser GNU Public
* License.
*
*/
#include <iostream>
#include <thread>
#include <chrono>
#include <cstring>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <getopt.h>
#include <mydev.h>
using namespace std;
void print_help(const char *, int);
typedef enum operation { OP_NONE, OP_MAPREAD, OP_MAPWRITE } operation_t;
#define OP_MAX 10
operation_t op[OP_MAX];
int op_cnt = 0;
char *dev_file = NULL;
int dev_fd = -1;
char *dev_mem = NULL;
int dev_oflag = 0;
char *msg = NULL;
int msg_len = 0;
int msg_buf_len = 0;
int main(int argc, char *argv[])
{
int c;
bool read_flag = false, write_flag = false;
int mmap_flags = 0;
int ret = 0;
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"help", 1, 0, 'h'},
{"ptype", 1, 0, 'p'},
{"message", 1, 0, 'm'},
{"operation", 1, 0, 'o'},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "hp:m:o:", long_options,
&option_index);
if (c == -1) //end of the command, getopt semantics
break;
switch (c) {
case 'h':
print_help(argv[0], -1);
break;
case 'm':
if (!optarg) {
print_help(argv[0], -1);
}
if (msg) {
cerr << "Duplicate message options\n";
print_help(argv[0], -1);
}
msg = optarg;
msg_len = strlen(optarg);
break;
case 'p':
if (!optarg) {
print_help(argv[0], -1);
}
if (strcmp("demand", optarg) == 0) {
//TODO mmap_flags = ??;
mmap_flags = MAP_SHARED;
} else if (strcmp("prefetch", optarg) == 0) {
//TODO mmap_flags = ??
mmap_flags = MAP_SHARED | MAP_POPULATE;
} else {
cerr << "Invalid operation: " << optarg << "\n";
print_help(argv[0], -1);
}
break;
case 'o':
if (!optarg) {
print_help(argv[0], -1);
}
if (op_cnt == OP_MAX) {
cerr << "Too many operations\n";
print_help(argv[0], -1);
}
if (strcmp("mapread", optarg) == 0) {
op[op_cnt] = OP_MAPREAD;
op_cnt++;
read_flag = true;
} else if (strcmp("mapwrite", optarg) == 0) {
op[op_cnt] = OP_MAPWRITE;
op_cnt++;
write_flag = true;
} else {
cerr << "Invalid operation: " << optarg << "\n";
print_help(argv[0], -1);
}
break;
case ':':
cerr << "Argument missing for option " <<
long_options[option_index].name << "\n";
print_help(argv[0], -1);
break;
case '?':
cerr << "Error, No such option\n";
print_help(argv[0], -1);
break;
default:
cerr << "?? getopt returned character code " << c <<
"\n";
}
}
if (optind > argc - 1) {
cerr << "Need devicemem argument\n";
print_help(argv[0], -1);
} else if (optind < argc - 1) {
cerr << "Too many arguments\n";
print_help(argv[0], -1);
}
dev_file = argv[optind];
// --message is not required with read alone
// --message is must along with write
if (write_flag) {
if (msg_len == 0 || msg == NULL) {
cerr << "Need message argument along with read/write\n";
print_help(argv[0], -1);
}
}
// Figure out appropriate oflag from --op
if (write_flag) {
dev_oflag = O_RDWR;
} else if (read_flag) {
dev_oflag = O_RDONLY;
} else { // just read or no operation
dev_oflag = O_RDONLY;
}
dev_fd = open(dev_file, dev_oflag, 0700);
if (dev_fd < 0) {
perror("mydev_open failed");
exit(-2);
}
for (int i = 0; i < op_cnt; i++) {
switch (op[i]) {
case OP_MAPREAD:{
off_t off = 0;
size_t len;
// memory map the devicemem's kernel buffer into user-space segment.
dev_mem =
(char *)mmap(NULL, MYDEV_LEN, PROT_READ,
mmap_flags, dev_fd, off);
// Checking if the memory mapping was successful
if (dev_mem == MAP_FAILED) {
perror("mmap read failed");
exit(3);
}
// If there is a message we need to read and ensure the correct message is being read
if (msg != NULL) {
do {
len = msg_len < MYDEV_LEN - off ? msg_len: MYDEV_LEN-off;
// Checking and assigning the smaller of the message length and total device length after deduction of the offset
if (len <= 0) { // If length is non positive the entire device memory is filled
break;
}
int i = 0;
while (i < len) { // Checking upto the length determined above if the message matches the message stored in the device memory
if (msg[i] !=
*(dev_mem + off +
i)) {
cerr << "Read error"; // If does not, a read error is ecountered
ret = -1;
}
i++;
}
off = off + len; // Updating the offset value to check for next segment of device memory
if (off == MYDEV_LEN) { // If the offset has reached the end of device memory, stop checking
break;
}
} while (true);
}
// Compare the data read from devicemem with msg
// DONOT define an array of MYDEV_LEN. Seriously! Dont need array of MYDEV_LEN size.
// TODO. Hint use loop & modulus operator on msg to compare the string with entire device_mem
munmap(dev_mem, MYDEV_LEN); // Unmammping
break;
}
// munmap(mem, MYDEV_LEN);
case OP_MAPWRITE:{
off_t off = 0;
size_t len;
// memory map the devicemem's kernel buffer into user-space segment.
dev_mem =
(char *)mmap(NULL, MYDEV_LEN, PROT_WRITE,
mmap_flags, dev_fd, off);
// TODO
if (dev_mem == MAP_FAILED) {
perror("mmap write failed");
exit(3);
}
// Write the message to devicemem from msg.
// DONOT define an array of MYDEV_LEN. Seriously! Dont need array of MYDEV_LEN size.
// As we have a message to write, we write it iteratively to the device memory such that it occupies all available space on that device, here 1MB
do {
len = msg_len < MYDEV_LEN - off ? msg_len: MYDEV_LEN-off; // Checking and assigning the smaller of the message length and total device length after deduction of the offset
if (len <= 0) { // If length is non positive the entire device memory is filled
break;
}
int i = 0;
while (i < len) {
*(dev_mem + off + i) = msg[i]; // Writing the message entered by the user to the device memory for this segment
i++;
}
off = off + len; // Updating the offset value to write to the next segment of device memory
if (off == MYDEV_LEN) { // If the offset has reached the end of device memory, stop writing
break;
}
} while (true);
// TODO. Hint use loop & modulus operator on msg to copy the string to entire device_mem
// unmap the devicemem's kernel buffer.
munmap(dev_mem, MYDEV_LEN); // Unmapping
break;
}
} // end switch
} // end for(ops)
close(dev_fd);
return ret;
}
void print_help(const char *name, int exit_value)
{
cerr << "Usage: " << name << " [options] <devname>\n";
cerr << "Options:\n";
cerr <<
"--operation <optype> : Where optype can be mapread, mapwrite\n";
cerr <<
"--message <message> : Message to be written/read-compare to/from the device memory\n";
cerr <<
"--paging <ptype> : Where ptype can be prefetch or demand\n";
cerr << "--help : Show this help\n";
exit(exit_value);
}