Permalink
Browse files

ramfs stuff, including userspace testapp

  • Loading branch information...
wolfwood committed Oct 10, 2009
1 parent 3155341 commit 7faa3c8df5174733449dbc5ee0622104525e3ef8
Showing with 254 additions and 78 deletions.
  1. +1 −0 .gitignore
  2. +10 −6 app/d/testapp.d
  3. +1 −0 build/iso/boot/grub/menu.lst
  4. +24 −0 build/iso/boot/text
  5. +14 −0 kernel/core/syscall.d
  6. +37 −69 kernel/filesystem/ramfs.d
  7. +97 −0 libos/ramfs.d
  8. +57 −0 user/ramfs.d
  9. +13 −3 user/syscall.d
View
@@ -8,3 +8,4 @@ build/dsss.conf
user/nativecall.d
*.swp
*.o
+*~
View
@@ -8,23 +8,27 @@ module testapp;
import user.syscall;
import user.console;
+import user.ramfs;
import libos.console;
+import libos.ramfs;
void main() {
- long foo = add(5,9);
- asm {
- mov R15, RAX;
- }
Console.initialize();
Console.backcolor = Console.Color.Black;
Console.forecolor = Console.Color.Green;
Console.clear();
-// Console.putString("Arrrr, welcome ye to XOmB.\n\n ... ... forgive me ability to rhyme.");
- Console.position((Console.width - 15) / 2, Console.height / 2);
Console.putString("Welcome to XOmB\n\n");
+ int fd = Open("/boot/text", 0, 0);
+
+ char[128] str;
+
+ while(read(fd, str, str.length) == str.length){
+ Console.putString(str);
+ }
+
for (;;) {}
}
@@ -129,3 +129,4 @@ kernel /boot/xomb
module /boot/testapp
module /boot/testc
#module /boot/test
+module /boot/text
View
@@ -0,0 +1,24 @@
+Copyright (c) 2007 - 2009
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are met:
+* * Redistributions of source code must retain the above copyright
+* notice, this list of conditions and the following disclaimer.
+* * Redistributions in binary form must reproduce the above copyright
+* notice, this list of conditions and the following disclaimer in the
+* documentation and/or other materials provided with the distribution.
+* * Neither the name of the <organization> nor the
+* names of its contributors may be used to endorse or promote products
+* derived from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
+* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
+* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
@@ -15,6 +15,9 @@ import kernel.mem.heap;
import kernel.core.error;
import kernel.core.kprintf;
+import user.ramfs;
+import kernel.filesystem.ramfs;
+
struct SyscallImplementations {
static:
public:
@@ -66,5 +69,16 @@ public:
SyscallError fork(out int ret, ForkArgs* params) {
return SyscallError.OK;
}
+
+ SyscallError open(out int ret, OpenArgs* params){
+ Inode* node = RamFS.open(params.path);
+
+ if(node !is null){
+ *params.node = node;
+ return SyscallError.OK;
+ }else{
+ return SyscallError.Failcopter;
+ }
+ }
}
View
@@ -7,101 +7,69 @@ import kernel.mem.heap;
import kernel.core.kprintf;
+import user.ramfs;
+
struct RamFS{
static:
public:
- alias void* PagePtr;
-
- struct Inode{
- uint refcount;
- uint reserved;
- PagePtr[508] directPtrs;
- IndirectPtrs* indirectPtr;
- DoubleIndirectPtrs* doubleIndirectPtr;
- TripleIndirectPtrs* tripleIndirectPtr;
-
- void mapRegion(void* addy, ulong length){
- //assert(length <= (directPtrs.length * 4096), "module too big to become a file");
- assert((cast(ulong)addy % 4096) == 0);
-
- uint i = 0;
-
- while((length > i*4096) && (i < directPtrs.length)){
- directPtrs[i] = cast(void*)(cast(ulong)addy + i*4096);
+ void mapRegion(Inode* inode, void* addy, ulong length){
+ //assert(length <= (directPtrs.length * 4096), "module too big to become a file");
+ assert((cast(ulong)addy % 4096) == 0);
+
+ uint i = 0;
+
+ while((length > i*4096) && (i < inode.directPtrs.length)){
+ inode.directPtrs[i] = cast(void*)(cast(ulong)addy + i*4096);
+ i++;
+ }
+
+ if(length > i*4096){
+ inode.indirectPtr = cast(IndirectPtrs*)Heap.allocPage();
+
+ while((length > i*4096) && (i < inode.directPtrs.length + inode.indirectPtr.ptrs.length)){
+ inode.indirectPtr.ptrs[i - inode.directPtrs.length] = cast(void*)(cast(ulong)addy + i*4096);
i++;
}
-
- if(length > i*4096){
- indirectPtr = cast(IndirectPtrs*)Heap.allocPage();
-
- while((length > i*4096) && (i < directPtrs.length + indirectPtr.ptrs.length)){
- indirectPtr.ptrs[i - directPtrs.length] = cast(void*)(cast(ulong)addy + i*4096);
- i++;
- }
-
- }
}
- }
-
- struct IndirectPtrs{
- PagePtr[512] ptrs;
- }
-
-
- struct DoubleIndirectPtrs{
- IndirectPtrs*[512] ptrs;
- }
-
- struct TripleIndirectPtrs{
- DoubleIndirectPtrs*[512] ptrs;
- }
-
- struct DirEntry{
- char[119] name;
- bool isDir;
-
- union PtrUnion{
- Inode* inode;
- DirPage* dirpage;
- }
- PtrUnion ptr;
}
- const uint NUM_DIR_ENTRIES = 32;
-
- struct DirPage{
- DirEntry[NUM_DIR_ENTRIES] entries;
- }
-
- static assert(Inode.sizeof == 4096);
- static assert(IndirectPtrs.sizeof == 4096);
- static assert(DoubleIndirectPtrs.sizeof == 4096);
- static assert(DirEntry.sizeof == 128);
- static assert(DirPage.sizeof == 4096);
-
DirPage* root;
-
+
ErrorVal initialize(){
root = cast(DirPage*)Heap.allocPage();
for(uint i = 0; i < (System.numModules < NUM_DIR_ENTRIES ? System.numModules : NUM_DIR_ENTRIES); i++){
- root.entries[i].name[0..System.moduleInfo[i].name.length] = System.moduleInfo[i].name[0..$];
+ root.entries[i].name = System.moduleInfo[i].name;
kprintfln!("{}")(root.entries[i].name);
-
+
root.entries[i].ptr.inode = cast(Inode*)Heap.allocPage();
root.entries[i].ptr.inode.refcount = 1;
+ root.entries[i].ptr.inode.length = System.moduleInfo[i].length;
+
+ root.entries[i].ptr.inode.isContiguous = true;
- root.entries[i].ptr.inode.mapRegion(System.moduleInfo[i].virtualStart,
+ mapRegion(root.entries[i].ptr.inode, System.moduleInfo[i].virtualStart,
System.moduleInfo[i].length);
}
-
+
return ErrorVal.Success;
}
+
+ Inode* open(char[] path){
+
+ for(uint i = 0; (i < root.entries.sizeof) && (root.entries[i].name !is null); i++){
+ if(root.entries[i].name == path && root.entries[i].isDir){
+ return root.entries[i].ptr.inode;
+ }
+ }
+
+ return null;
+ }
}// end namespace foo
View
@@ -0,0 +1,97 @@
+
+module libos.ramfs;
+
+import user.ramfs;
+import user.syscall;
+
+extern(C) void* memcpy(void*, void*, size_t);
+
+struct FD{
+ Inode* inode;
+ ulong offset;
+}
+
+FD[256] fdtable;
+
+// open
+int Open(char[] path, int flags, int mode){
+
+ // XXX: not the least bit threadsafe... this is bad
+ for(uint i = 3; i < fdtable.length; i++){
+ if(fdtable[i].inode is null){
+ int err = open(path, &fdtable[i].inode);
+
+ if(err != SyscallError.OK){
+ return -1;
+ }else{
+ return i;
+ }
+ }
+ }
+
+ return -1; // XXX: EMFILE
+}
+
+// close
+int close(int f){
+ if(fdtable[f].inode is null){
+ return -1;
+ }else{
+ fdtable[f].offset = 0;
+ fdtable[f].inode = null;
+
+ return 0;
+ }
+}
+
+// read
+int read(int file, char[] ptr, int len) {
+ assert(fdtable[file].inode !is null);
+
+ if(fdtable[file].inode.length < (fdtable[file].offset + len)){
+ len = fdtable[file].inode.length - fdtable[file].offset;
+ }
+
+ if(fdtable[file].inode.isContiguous){
+
+ memcpy(cast(void*)(cast(ulong)fdtable[file].inode.directPtrs[0] + fdtable[file].offset),
+ ptr.ptr, len);
+
+ return len;
+ }else{
+ // XXX: implement inode traversal and copying in 4k chunks
+
+ return -1;
+ }
+}
+
+// write
+
+
+enum Whence { SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2}
+
+// lseek
+int lseek(int file, int ptr, int dir) {
+ assert(fdtable[file].inode !is null);
+
+ // XXX guard against overflowing file length
+ switch(cast(Whence)dir){
+ case Whence.SEEK_SET:
+ fdtable[file].offset = ptr;
+ break;
+ case Whence.SEEK_CUR:
+ fdtable[file].offset += ptr;
+ break;
+ case Whence.SEEK_END:
+ fdtable[file].offset = fdtable[file].inode.length - ptr;
+ break;
+ }
+
+ return 0;
+}
+
+// link
+
+
+// unlink
+
View
@@ -0,0 +1,57 @@
+
+module user.ramfs;
+
+
+alias void* PagePtr;
+
+struct Inode{
+ uint refcount;
+ bool isContiguous;
+ bool[3] reserved;
+ ulong length;
+
+ PagePtr[507] directPtrs;
+ IndirectPtrs* indirectPtr;
+ DoubleIndirectPtrs* doubleIndirectPtr;
+ TripleIndirectPtrs* tripleIndirectPtr;
+}
+
+struct IndirectPtrs{
+ PagePtr[512] ptrs;
+}
+
+
+struct DoubleIndirectPtrs{
+ IndirectPtrs*[512] ptrs;
+}
+
+struct TripleIndirectPtrs{
+ DoubleIndirectPtrs*[512] ptrs;
+}
+
+struct DirEntry{
+ char[] name;
+ bool isDir;
+ bool[3] reserved;
+
+
+ union PtrUnion{
+ Inode* inode;
+ DirPage* dirpage;
+ }
+
+ PtrUnion ptr;
+}
+
+const uint NUM_DIR_ENTRIES = 4096 / DirEntry.sizeof;
+
+struct DirPage{
+ DirEntry[NUM_DIR_ENTRIES] entries;
+}
+
+static assert(Inode.sizeof == 4096);
+static assert(IndirectPtrs.sizeof == 4096);
+static assert(DoubleIndirectPtrs.sizeof == 4096);
+//static assert(DirEntry.sizeof == 32);
+static assert(DirPage.sizeof == 4096);
+
Oops, something went wrong.

0 comments on commit 7faa3c8

Please sign in to comment.