feat(userspace): add demo ring-3 programs
This commit is contained in:
1 parent
b15dfc048a
commit
d4bc2a209f
4 files changed
+135
No files matched your search
@@ -0,0 +1,18 @@
|
|||||||
|
#include "ulib.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
long pid = userGetpid();
|
||||||
|
|
||||||
|
for (int step = 0; step < 5; step++) {
|
||||||
|
print("counter: pid=");
|
||||||
|
printDec(pid);
|
||||||
|
print(" step=");
|
||||||
|
printDec(step);
|
||||||
|
print("\n");
|
||||||
|
userSleep(30);
|
||||||
|
}
|
||||||
|
|
||||||
|
print("counter: finished counting\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
#include "ulib.h"
|
||||||
|
#include "fs.h"
|
||||||
|
|
||||||
|
static const char* motdPath = "/etc/motd.txt";
|
||||||
|
static const char* notePath = "/tmp/note.txt";
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
print("fileread: start, pid=");
|
||||||
|
printDec(userGetpid());
|
||||||
|
print("\n");
|
||||||
|
|
||||||
|
FbStat st;
|
||||||
|
if (userStat(motdPath, &st) == 0) {
|
||||||
|
print("fileread: stat ");
|
||||||
|
print(motdPath);
|
||||||
|
print(" size=");
|
||||||
|
printDec((long)st.size);
|
||||||
|
print("\n");
|
||||||
|
} else {
|
||||||
|
print("fileread: stat failed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd = (int)userOpen(motdPath, oReadOnly);
|
||||||
|
if (fd < 0) {
|
||||||
|
print("fileread: open failed\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
print("fileread: --- begin ");
|
||||||
|
print(motdPath);
|
||||||
|
print(" ---\n");
|
||||||
|
char buffer[128];
|
||||||
|
long n;
|
||||||
|
while ((n = userRead(fd, buffer, sizeof(buffer))) > 0)
|
||||||
|
userWrite(1, buffer, (unsigned long)n);
|
||||||
|
print("fileread: --- end ---\n");
|
||||||
|
|
||||||
|
userLseek(fd, 0, seekSet);
|
||||||
|
n = userRead(fd, buffer, 16);
|
||||||
|
print("fileread: lseek reread first bytes: ");
|
||||||
|
userWrite(1, buffer, (unsigned long)n);
|
||||||
|
print("\n");
|
||||||
|
userClose(fd);
|
||||||
|
|
||||||
|
int wfd = (int)userOpen(notePath, oReadWrite | oCreate | oTrunc);
|
||||||
|
if (wfd < 0) {
|
||||||
|
print("fileread: tmpfs open failed\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
const char* payload = "frostbyte-tmpfs-rw-ok\n";
|
||||||
|
userWrite(wfd, payload, stringLength(payload));
|
||||||
|
userLseek(wfd, 0, seekSet);
|
||||||
|
char readback[64];
|
||||||
|
long rn = userRead(wfd, readback, sizeof(readback));
|
||||||
|
print("fileread: tmpfs read back: ");
|
||||||
|
userWrite(1, readback, (unsigned long)rn);
|
||||||
|
userClose(wfd);
|
||||||
|
|
||||||
|
print("fileread: listing /etc via getdents:\n");
|
||||||
|
int dfd = (int)userOpen("/etc", oReadOnly);
|
||||||
|
if (dfd >= 0) {
|
||||||
|
FbDirent entries[8];
|
||||||
|
long bytes = userGetdents(dfd, entries, sizeof(entries));
|
||||||
|
long records = bytes / (long)sizeof(FbDirent);
|
||||||
|
for (long i = 0; i < records; i++) {
|
||||||
|
print(" ");
|
||||||
|
print(entries[i].name);
|
||||||
|
print(entries[i].type == fsTypeDir ? "/" : "");
|
||||||
|
print("\n");
|
||||||
|
}
|
||||||
|
userClose(dfd);
|
||||||
|
}
|
||||||
|
|
||||||
|
print("fileread: done\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#include "ulib.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
print("hello: greetings from ring 3, pid=");
|
||||||
|
printDec(userGetpid());
|
||||||
|
print("\n");
|
||||||
|
|
||||||
|
userYield();
|
||||||
|
|
||||||
|
print("hello: resumed after yield, exiting cleanly\n");
|
||||||
|
return 7;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#include "ulib.h"
|
||||||
|
|
||||||
|
#define blockBytes 8192
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
print("sbrktest: pid=");
|
||||||
|
printDec(userGetpid());
|
||||||
|
print(" requesting 8192 bytes via sbrk\n");
|
||||||
|
|
||||||
|
char* block = (char*)userSbrk(blockBytes);
|
||||||
|
|
||||||
|
for (int i = 0; i < blockBytes; i++)
|
||||||
|
block[i] = (char)(i & 0x7F);
|
||||||
|
|
||||||
|
int intact = 1;
|
||||||
|
for (int i = 0; i < blockBytes; i++)
|
||||||
|
if (block[i] != (char)(i & 0x7F))
|
||||||
|
intact = 0;
|
||||||
|
|
||||||
|
if (intact)
|
||||||
|
print("sbrktest: heap page write and read-back verified OK\n");
|
||||||
|
else
|
||||||
|
print("sbrktest: heap verification FAILED\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user