-
-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathsystemio.h
More file actions
457 lines (392 loc) · 14.7 KB
/
Copy pathsystemio.h
File metadata and controls
457 lines (392 loc) · 14.7 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#pragma once
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QInputDialog>
#include <QMutex>
#include <QObject>
#include <QTemporaryFile>
#include <QTextStream>
#include <QWaitCondition>
#include <stdexcept>
#include <sys/stat.h>
#include "STLExtras.h"
#include "statusmanager.h"
namespace Ripes {
/*
Copyright (c) 2003-2013, Pete Sanderson and Kenneth Vollmar
Developed by Pete Sanderson (psanderson@otterbein.edu)
and Kenneth Vollmar (kenvollmar@missouristate.edu)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(MIT license, http://www.opensource.org/licenses/mit-license.html)
*/
/**
* @brief The SystemIO class
* Provides standard i/o services needed to simulate the RISCV syscall
* routines.
* This class is largely based on the SystemIO.java class of RARS.
*/
class SystemIO : public QObject {
Q_OBJECT
public:
static SystemIO &get() {
static SystemIO sio;
return sio;
}
private:
// String used for description of file error
static QString s_fileErrorString; // = ("File operation OK");
// Flag used for aborting waiting for I/O
static bool s_abortSyscall;
// Standard I/O Channels
enum STDIO { STDIN = 0, STDOUT = 1, STDERR = 2, STDIO_END };
// Buffer size for syscalls for file I/O
static constexpr int SYSCALL_BUFSIZE = 128;
// Maximum number of files that can be open
static constexpr int SYSCALL_MAXFILES = 32;
static constexpr int O_RDONLY = 0x00000000;
static constexpr int O_WRONLY = 0x00000001;
static constexpr int O_RDWR = 0x00000002;
static constexpr int O_APPEND = 0x00000008;
static constexpr int O_CREAT = 0x00000200; // 512
static constexpr int O_TRUNC = 0x00000400; // 1024
static constexpr int O_EXCL = 0x00000800; // 2048
enum Permission {
WRITE_PERMS,
READ_PERMS,
};
// //////////////////////////////////////////////////////////////////////////////
// Maintain information on files in use. The index to the arrays is the "file
// descriptor."
struct FileIOData {
// The filenames in use. Null if file descriptor i is not in use.
static std::map<int, QString> fileNames;
// The flags of this file, 0=READ, 1=WRITE. Invalid if this file descriptor
// is not in use.
static std::map<int, unsigned> fileFlags;
// The streams in use, associated with the filenames
static std::map<int, QTextStream> streams;
// The file pointers in use
static std::map<int, QFile> files;
// QByteArray to use as a stdin buffer
static QByteArray s_stdinBuffer;
/**
* @brief s_stdioMutex
* Used for implementing the waitCondition between the producer/consumer
* scenario where ecall handling is blocking while waiting for the
* stdinBuffer to be non-empty.
*/
static QMutex s_stdioMutex;
static QWaitCondition s_stdinBufferEmpty;
// Reset all file information. Closes any open files and resets the arrays
static void resetFiles() {
for (int i = 0; i < SYSCALL_MAXFILES; ++i) {
close(i);
}
setupStdio();
}
static void setupStdio() {
fileNames[STDIN] = "STDIN";
fileNames[STDOUT] = "STDOUT";
fileNames[STDERR] = "STDERR";
fileFlags[STDIN] = SystemIO::O_RDONLY;
fileFlags[STDOUT] = SystemIO::O_WRONLY;
fileFlags[STDERR] = SystemIO::O_WRONLY;
if (streams.count(STDIN) == 0) {
// stdin stream has not yet been created
streams.emplace(STDIN, &s_stdinBuffer);
} else {
// Clear stdin stream and reset stream
s_stdinBuffer.clear();
auto success = streams[STDIN].seek(0);
Q_ASSERT(success);
}
// stdout/stderr will be handled via. signal/slots internally in the
// application streams.emplace(STDOUT, stdout); streams.emplace(STDERR,
// stderr);
}
// Open a file stream assigned to the given file descriptor
static void openFilestream(int fd, const QString &filename) {
files.emplace(fd, filename);
const auto flags = fileFlags[fd];
const auto qtOpenFlags = // Translate from stdlib file flags to Qt flags
(flags & O_RDONLY ? QIODevice::ReadOnly : QIODevice::NotOpen) |
(flags & O_WRONLY ? QIODevice::WriteOnly : QIODevice::NotOpen) |
(flags & O_RDWR ? QIODevice::ReadWrite : QIODevice::NotOpen) |
(flags & O_TRUNC ? QIODevice::Truncate : QIODevice::Append) |
(flags & O_EXCL ? QIODevice::NewOnly : QIODevice::NotOpen);
// Try to open file with the given flags
files[fd].open(qtOpenFlags);
if (!files[fd].exists() && flags & O_CREAT) {
files.erase(fd);
throw std::runtime_error("Could not create file");
}
if (!files[fd].exists()) {
files.erase(fd);
throw std::runtime_error("File not found");
}
if (!files[fd].isOpen()) {
files.erase(fd);
throw std::runtime_error("File could not be opened");
}
streams.emplace(fd, &files[fd]);
}
// Retrieve a stream for use
static QTextStream &getStreamInUse(int fd) { return streams[fd]; }
// Determine whether a given filename is already in use.
static bool filenameInUse(const QString &requestedFilename) {
return llvm::any_of(fileNames, [&](auto fn) {
return !fn.second.isEmpty() && fn.second == requestedFilename;
});
}
// Determine whether a given fd is already in use with either Read or Write permissions
static bool fdInUse(int fd, Permission perm) {
if (fd < 0 || fd >= SYSCALL_MAXFILES) {
return false;
} else if (fileNames[fd].isEmpty()) {
return false;
} else if (perm == READ_PERMS) {
return (fileFlags[fd] + 1) & 1; // LSB is 1 if the fd has read perms
}
return (fileFlags[fd] + 1) & 2; // 2nd LSB is 1 if the fd has write perms
}
// Close the file with file descriptor fd. No errors are recoverable -- if
// the user's made an error in the call, it will come back to him.
static void close(int fd) {
// Can't close STDIN, STDOUT, STDERR, or invalid fd
if (fd < STDIO_END || fd >= SYSCALL_MAXFILES)
return;
fileFlags[fd] = -1;
files[fd].close();
streams.erase(fd);
files.erase(fd);
fileNames.erase(fd);
}
// Attempt to open a new file with the given flag, using the lowest
// available file descriptor. Check that filename is not in use, flag is
// reasonable, and there is an available file descriptor. Return: file
// descriptor in 0...(SYSCALL_MAXFILES-1), or -1 if error
static int nowOpening(const QString &filename, int flag) {
int i = 0;
if (filenameInUse(filename)) {
s_fileErrorString = "File name " + filename + " is already open.";
return -1;
}
while (!fileNames[i].isEmpty() && i < SYSCALL_MAXFILES) {
i++;
} // Attempt to find available file descriptor
if (i >= SYSCALL_MAXFILES) // no available file descriptors
{
s_fileErrorString = "File name " + filename +
" exceeds maximum open file limit of " +
QString::number(SYSCALL_MAXFILES);
return -1;
}
// Must be OK -- put filename in table
fileNames[i] = filename; // our table has its own copy of filename
fileFlags[i] = flag;
s_fileErrorString = "File operation OK";
return i;
}
};
public:
/**
* Open a file for either reading or writing.
*
* @param filename string containing filename
* @param flags 0 for read, 1 for write
* @return file descriptor in the range 0 to SYSCALL_MAXFILES-1, or -1 if
* error
*/
static int openFile(QString filename, int flags) {
SystemIO::get(); // Ensure that SystemIO is constructed
// Internally, a "file descriptor" is an index into a table
// of the filename, flag, and the File???putStream associated with
// that file descriptor.
int retValue = -1;
int fdToUse;
// Check internal plausibility of opening this file
fdToUse = FileIOData::nowOpening(filename, flags);
retValue = fdToUse; // return value is the fd
if (fdToUse < 0) {
return -1;
} // fileErrorString would have been set
try {
FileIOData::openFilestream(fdToUse, filename);
} catch (int) {
s_fileErrorString = "File " + filename + " could not be opened.";
retValue = -1;
}
return retValue; // return the "file descriptor"
}
/**
* Read bytes from file.
*
* @param fd file descriptor
* @param offset where in the file to seek to
* @param base the point to reference 0 for start of file, 1 for current
* position, 2 for end of the file
* @return -1 on error
*/
static int seek(int fd, int offset, int base) {
SystemIO::get(); // Ensure that SystemIO is constructed
if (!FileIOData::fdInUse(fd, READ_PERMS)) // Check the existence of the "read" fd
{
s_fileErrorString =
"File descriptor " + QString::number(fd) + " is not open for reading";
return -1;
}
if (fd < 0 || fd >= SYSCALL_MAXFILES)
return -1;
auto &stream = FileIOData::getStreamInUse(fd);
if (base == SEEK_SET) {
offset += 0;
} else if (base == SEEK_CUR) {
offset += stream.pos();
} else if (base == SEEK_END) {
offset += FileIOData::files[fd].size();
} else {
return -1;
}
if (offset < 0) {
return -1;
}
stream.seek(offset);
return offset;
}
/**
* Read bytes from file.
*
* @param fd file descriptor
* @param myBuffer QString to contain bytes read
* @param lengthRequested number of bytes to read
* @return number of bytes read, 0 on EOF, or -1 on error
*/
static int readFromFile(int fd, QByteArray &myBuffer, int lengthRequested) {
s_abortSyscall = false; // Reset any stale abort requests
SystemIO::get(); // Ensure that SystemIO is constructed
/////////////// DPS 8-Jan-2013
/////////////////////////////////////////////////////
/// Read from STDIN file descriptor while using IDE - get input from
/// Messages pane.
if (!FileIOData::fdInUse(fd,
READ_PERMS)) // Check the existence of the "read" fd
{
s_fileErrorString =
"File descriptor " + QString::number(fd) + " is not open for reading";
return -1;
}
// retrieve FileInputStream from storage
auto &InputStream = FileIOData::getStreamInUse(fd);
if (fd == STDIN) {
// systemIO might be called from non-gui thread, so be threadsafe in
// interacting with the ui.
postToGUIThread([=] {
SystemIOStatusManager::setStatusTimed("Waiting for user input...",
99999999);
});
while (myBuffer.size() < lengthRequested) {
// Lock the stdio objects and try to read from stdio. If no data is
// present, wait until so.
FileIOData::s_stdioMutex.lock();
if (s_abortSyscall) {
FileIOData::s_stdioMutex.unlock();
s_abortSyscall = false;
postToGUIThread([=] { SystemIOStatusManager::clearStatus(); });
return -1;
}
auto readData = InputStream.read(lengthRequested).toUtf8();
myBuffer.append(readData);
lengthRequested -= readData.length();
/** We spin on a wait condition with a timeout. The timeout is required
* to ensure that we may observe any abort flags (ie. if execution is
* stopped while waiting for IO */
FileIOData::s_stdinBufferEmpty.wait(&FileIOData::s_stdioMutex, 100);
FileIOData::s_stdioMutex.unlock();
if (myBuffer.endsWith('\n'))
break;
}
} else {
// Reads up to lengthRequested bytes of data from this Input stream into
// an array of bytes.
myBuffer = InputStream.read(lengthRequested).toUtf8();
}
if (myBuffer.size() == 0) {
if (fd == STDIN) {
Q_ASSERT(false); // EOF should never be possible for STDIN
} else {
// End of file - write EOF file character into buffer
myBuffer.append(sizeof(int), EOF);
}
}
postToGUIThread([=] { SystemIOStatusManager::clearStatus(); });
return myBuffer.size();
} // end readFromFile
/**
* Write bytes to file.
*
* @param fd file descriptor
* @param myBuffer byte array containing characters to write
* @param lengthRequested number of bytes to write
* @return number of bytes written, or -1 on error
*/
static int writeToFile(int fd, const QString &myBuffer, int lengthRequested) {
SystemIO::get(); // Ensure that SystemIO is constructed
if (fd == STDOUT || fd == STDERR) {
emit get().doPrint(myBuffer);
return myBuffer.size();
}
if (!FileIOData::fdInUse(
fd, WRITE_PERMS)) // Check the existence of the "write" fd
{
s_fileErrorString =
"File descriptor " + QString::number(fd) + " is not open for writing";
return -1;
}
// retrieve FileOutputStream from storage
auto &outputStream = FileIOData::getStreamInUse(fd);
outputStream << myBuffer;
outputStream.flush();
return lengthRequested;
} // end writeToFile
/**
* Close the file with specified file descriptor
*
* @param fd the file descriptor of an open file
*/
static void closeFile(int fd) { FileIOData::close(fd); }
static void printString(const QString &string) { emit get().doPrint(string); }
static void reset() { FileIOData::resetFiles(); }
static void abortSyscall() { s_abortSyscall = true; }
signals:
void doPrint(const QString &);
public slots:
/**
* @brief putStdInData
* Pushes @p data onto the stdin buffer object
*/
void putStdInData(const QByteArray &data) {
FileIOData::s_stdioMutex.lock();
FileIOData::s_stdinBuffer.append(data);
FileIOData::s_stdinBufferEmpty.wakeAll();
FileIOData::s_stdioMutex.unlock();
}
private:
SystemIO() { reset(); }
};
} // namespace Ripes