vfs, module loading and string table

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
FlashBurn

vfs, module loading and string table

Post by FlashBurn »

At the moment I design my vfs layer for module and program loading and now there are some problems I have :(

I need a good way to make a string table of all the labels I export in the kernel (with their addresses). I don?t know how I should organize this table. I?m open to every solution, because I will write this table by hand.

Next thing is the vfs layer. I will use the copy-on-write method for modules and programs. Both will stay in kernelspace, the memory waste is small and I have a good way for file parsing, because the file struc is untouched in kernelspace.

The problem I have with my vfs layer is the size of the filenames. I don?t know if 256 (with "\0") is enough, but as I searched this seems to be the limit of most filesystems!?

If you want you can also present your vfs design and here the same I?m open for every idea!
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:vfs, module loading and string table

Post by Pype.Clicker »

The best way around for symbols table is a hash. Each of your symbols name is given to a hash function (think of CRC16 over the name: that'll be a good example) and you insert items in the table entry of their own hash value.

Now, if i were you, i'd try to avoid doing this by hand. You might e.g. use an "export(x) x" macro and grep your source for " export\(.*\) "to get all the names the kernel/a module exports, then compare this information with the linker map (ld -M -map <mapfile>) or the output of "nm <yourstuff.o>" to know their addresses. A perl script would probably be ideal for that...

You're not even obliged to use the output of nm! Once you've computed the structure of the hashtable, i suggest you generate a source file (exports.c, for instance) that will contain what you'd have written by hand, e.g.

Code: Select all

void kprintf();
void kalloc();

symtbl[]={
    [0x123] : {"kprintf",kprintf};
    [0x264] : {"kalloc",kalloc};
};
If generating the hashes (e.g. 0x123 and 0x264) in the scripts sounds overkill to you, you can keep the symbol table unsorted in the file and insert stuff in the hash table at run-time.

If you use ELF file format, you might want to put the symbol table (and possibly other information) in a specific section of the file...

You may like to have a look at http://clicker.sf.net/wiclicker/index.p ... esTutorial
http://clicker.sf.net/wiclicker/index.php/KmodFormat
http://clicker.sf.net/wiclicker/index.p ... lsCmPlugin
if you wish to see how this works in Clicker.

About max-256-chars filename: that sounds perfectly reasonable to me ...
FlashBurn

Re:vfs, module loading and string table

Post by FlashBurn »

Ok, I forgot to say that I program in assembly, so the only way to generate the string table is by hand and with the help of macros! I use the elf format, but not for the kernel (for modules and programs). With the kernel I have the problem that the sections are there where the .bss section is (in memory) and so I will stay with my own binary format and I needn?t to write an elf parser for my loader ;)

The problem with hashes is that it wouldn?t be easy (if it is possible) to do this in the compiler (fasm). I thought of a alphabetically sorted list and maybe an array of 26 items, one entry (address) for every letter?!

The method should be fast for a small amount of entries and for a huge amount of entries. Also my labels tend to be very long.

A variant I used in another projects is this:

Code: Select all

dd ptr 2 next entry
dd addr
db 0-terminated string
This with an array should be a good solution to start with, shouldn?t it?!
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:vfs, module loading and string table

Post by Pype.Clicker »

if your labels are very long (and especially if they're organized in a namespace_subspace_method way), you may want to group them by namespace, e.g. rather than having a flat list, you'd have a "tree" structure where all symbols with e.g. paging_* would be under a "paging" node.

That'd mean if you're looking (in a module) for "paging_create_table", you'd first look for "paging" in the top-level list, then for "create" in the "paging_*" list, and finally for "table" in "paging_create_*" list.

That will most likely shorten your searching time (shorter strings to compare) and might also reduce memory footprint (if you have enough symbols sharing the same prefix so that the cost for the prefix entry is "ammortized").
The problem with hashes is that it wouldn?t be easy (if it is possible) to do this in the compiler (fasm)
You're not forced to do them in the compiler proper: just have an additional tool that scan your sources, hash the symbols and produce the exporthashtable.asm that you'll later compile and link against the rest ;)
I use the elf format, but not for the kernel (for modules and programs)
Maybe i underestimate your compiling sequence, but if you're just using -oformat binary, nothing should prevent you to create a big kernel.elf, grab symbols from there and _then_ objcopy the kernel.elf to kernel.bin ...

Or maybe you may want macros that define the symbol locally, but also add the "symbol table entry" in a specific section (that's the advantage of using asm: you'll have a better control over sections) so that they end up as a table when compilation is over.

e.g.

Code: Select all

#macro kexport(symbol)
.section ksymnames
kst_##symbol
db #stringify(symbol),0
.section ksymtab
dd symbol
dd kst_##symbol
.section text
symbol:
#endmacro
where i suppose symbol1##symbol2 creates a concatened symbol name on your assembler's preprocessor and that #stringify(xyz) is preprocessed into 'xyz'.
If that's not possible, we're in trouble here and the 'macro' trick cannot be used.

Btw, trust me when i say that, with >40 symbols in your kernel, maintaining a symbol table by hand is a nightmare (not even talking about typos, etc.)
FlashBurn

Re:vfs, module loading and string table

Post by FlashBurn »

The idea with the namespace is good, I will take it ;D

I think that elf is to much overhead for my kernel, it is better if I have my own format, so I can control what goes into the binary (to keep it small) and how the file is organized.

At the moment I don?t know how much labels I have to export, but I think I will stay with the by hand method (with the help of macros). Because I have reserved a whole page for the string table I think I can align the strings to 4byte and this should give me another little speed-up.

I?m still at the designing state of my vfs layer and so I can?t realy test the code :( And this weekend seems to be good weather over here and this means no time for coding or designing!
FlashBurn

Re:vfs, module loading and string table

Post by FlashBurn »

I?ve written a function for getting the address of a given label, but it doesn?t work as expected :(

The problem is when I have a sub space table like this:

Code: Select all

func1
func2
And I give the string "func" then the function goes into an endless loop, but why?

Code: Select all

struc strTable_nameSpace_t
{
   .next               rd 1
   .childs               rd 1
   .str               rb 0
}

struc strTable_subSpace_t
{
   .next               rd 1
   .addr               rd 1
   .str               rb 0
}

;----------------------------
PROC strTable_get_addr, ptr2str
;----------------------------
BEGIN
;----------------------------
;   init all regs
   mov esi,[ptr2str]
   xor ecx,ecx
   mov edx,strTable_nameSpace_t.str
   mov edi,str_table_start
;----------------------------
;   main loop for comparing the namespace token of the string
align 4
.loop@namespace:
   mov al,[esi+ecx]
   
   test al,al
   jz .end_err
   
   cmp [edi+edx],al
   jne .loop@namespace@next
   
   add ecx,1
   add edx,1
   
   cmp byte[edi+edx],0
   jne .loop@namespace
;----------------------------
;   preface of the subspace mail loop
   mov eax,[edi+strTable_nameSpace_t.childs]
   mov edx,strTable_subSpace_t.str
   mov ebx,ecx
   mov edi,eax
      
   jmp .loop@subspace
;----------------------------
;   get the ptr 2 the next entry in the namespace
align 4
.loop@namespace@next:
   mov eax,[edi+strTable_nameSpace_t.next]
   
   test eax,eax
   jz .end_err
   
   mov edx,strTable_nameSpace_t.str
   xor ecx,ecx
   mov edi,eax
;----------------------------
;   if the ascii code in the namespace item is bigger than the one in al we needn?t to go on
   mov al,[esi+ecx]
   cmp [edi+edx],al
   jg .end_err
   
   jmp .loop@namespace
;----------------------------
;   main loop of the subspace token
align 4
.loop@subspace:
   mov al,[esi+ecx]
   
   cmp [edi+edx],al
   jne .loop@subspace@next
   
   test al,al
   jz .end
   
   add ecx,1
   add edx,1
   
   jmp .loop@subspace
;----------------------------
;   get the ptr 2 the next entry in the subspace
align 4
.loop@subspace@next:
   mov eax,[edi+strTable_subSpace_t.next]
   
   test eax,eax
   jz .end_err
   
   mov edx,strTable_subSpace_t.str
   mov ecx,ebx
;----------------------------
;   if the ascii code in the namespace item is bigger than the one in al we needn?t to go on   
   mov al,[esi+ecx]
   cmp [edi+edx],al
   jg .end_err
   
   jmp .loop@subspace
;----------------------------
;   return the addr of the label
align 4
.end:
   mov eax,[edi+strTable_subSpace_t.addr]
   RETURN
;----------------------------
;   return an error
align 4
.end_err:
   RETURN 0
ENDP
;----------------------------
FlashBurn

Re:vfs, module loading and string table

Post by FlashBurn »

Ok, I didn?t look at the code till today and now I found the error, I forgot a "mov edi,eax" in the part ".loop@subspace@next"!

Code: Select all

;----------------------------
PROC strTable_get_addr, ptr2str
;----------------------------
BEGIN
;----------------------------
;   init all regs
   mov esi,[ptr2str]
   xor ecx,ecx
   mov edx,strTable_nameSpace_t.string
   mov edi,str_table_start
;----------------------------
;   main loop for comparing the namespace token of the string
align 4
.loop@namespace:
   mov al,[esi+ecx]

   test al,al
   jz .end_err

   cmp [edi+edx],al
   jne .loop@namespace@next

   add ecx,1
   add edx,1

   cmp byte[edi+edx],0
   jne .loop@namespace
;----------------------------
;   preface of the subspace mail loop
   mov eax,[edi+strTable_nameSpace_t.childs]
   mov edx,strTable_subSpace_t.string
   mov ebx,ecx
   mov edi,eax

   jmp .loop@subspace
;----------------------------
;   get the ptr 2 the next entry in the namespace
align 4
.loop@namespace@next:
   mov eax,[edi+strTable_nameSpace_t.next]

   test eax,eax
   jz .end_err

   mov edx,strTable_nameSpace_t.string
   xor ecx,ecx
   mov edi,eax
;----------------------------
;   if the ascii code in the namespace item is bigger than the one in al we needn?t to go on
   mov al,[esi+ecx]
   cmp [edi+edx],al
   jg .end_err

   jmp .loop@namespace
;----------------------------
;   main loop of the subspace token
align 4
.loop@subspace:
   mov al,[esi+ecx]

   cmp [edi+edx],al
   jne .loop@subspace@next

   test al,al
   jz .end

   add ecx,1
   add edx,1

   jmp .loop@subspace
;----------------------------
;   get the ptr 2 the next entry in the subspace
align 4
.loop@subspace@next:
   mov eax,[edi+strTable_subSpace_t.next]

   test eax,eax
   jz .end_err

   mov edx,strTable_subSpace_t.string
   mov ecx,ebx
   mov edi,eax               ;<- this I forgot
;----------------------------
;   if the ascii code in the namespace item is bigger than the one in al we needn?t to go on
   mov al,[esi+ecx]
   cmp [edi+edx],al
   jg .end_err

   jmp .loop@subspace
;----------------------------
;   return the addr of the label
align 4
.end:
   mov eax,[edi+strTable_subSpace_t.addr]
   RETURN
;----------------------------
;   return an error
align 4
.end_err:
   RETURN 0
ENDP
;----------------------------
I think this is the fastest possible function with the tables, isn?t it?!
Ryu

Re:vfs, module loading and string table

Post by Ryu »

Pype.Clicker wrote: Or maybe you may want macros that define the symbol locally, but also add the "symbol table entry" in a specific section (that's the advantage of using asm: you'll have a better control over sections) so that they end up as a table when compilation is over.
For kernel specifically, I have in mind something simular with this to export all public symbols over external modules. Only thing is I think I will make a single source file with the section that creates all exported symbols to ensure that the orders are the same, for the sake of backwards compatibility as the kernel is constantly changing. On top of that I will have to create a lib file for the importing modules. (I guess this is no differnt from .bss)

Just a thought, as I may be missing the whole point of this string table, depending if your looking to support dynamic linked modules or is this for exporting kernel specifically. For modules that the kernel depends on, such as driver/server modules I have in mind a specific section that is exporting interface calls in a specific order so no string parsing is required. For dynamic link modules theres probably no way out parsing string tables though..
Post Reply