Bona Fide OS Developer
View unanswered posts | View active topics It is currently Thu Mar 28, 2024 12:49 pm



Post new topic Reply to topic  [ 67 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6, 7  Next
 How do i get my kernel to load binary programs? 
Author Message
Post Re: How do i get my kernel to load binary programs?
i dont think you get it:
is my kernel is occupying offset 256
so loading a program with an offset of 256 will overwrite the kernel
and crash when the child returns.


Thu Oct 08, 2009 4:40 pm

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
thanks to all this time spending on pointers it dawned on me that I have another serious bug on Y1OS only apperent when programs get large enought

anywho... what I recommend to you is to allocate memory that the kernel isn't using... then do some segment offset math....

this is the equation everything is based on
Code:
flat_address = segment*16 + offset;

first get the the pointer returned by malloc
Code:
old_pointer = (void *) malloc(size_of_file);

force the new offset to equal 256;
Code:
 OFFSET(new_pointer) = 256;
 SEGMENT(new_pointer) = SEGMENT(old_pointer) - (256 - OFFSET(old_pointer)) / 16;


and to help see things better all of the following addresses are equal

Code:
00FF:0000h
00FE:0010h
00FD:0020h
00EF:0100h <- what you want


if ordinarily malloc returns something that is not paragraph aligned (ie: the offset is not divisible by 16) then in such cases you allocate an extra 16 bytes and make a pointer that is paragraph aligned

Code:
buff_pointer = malloc(size_of_file + 16);
// say buff_pointer now holds address 00FF:0004h
// make old_pointer paragraph aligned (1 paragraph = 16 bytes)
old_pointer = buff_pointer + 12;
// old_pointer equals 00FF:0010h
// now that the offset of old_pointer is divisible by 16 do the above
OFFSET(new_pointer) = 256;
SEGMENT(new_pointer) = SEGMENT(old_pointer) - (256 - OFFSET(old_pointer)) / 16;
// you may now load the file onto new pointer
fread(new_pointer, 1, size_of_file, f);


Thu Oct 08, 2009 6:22 pm
Profile
Post Re: How do i get my kernel to load binary programs?
thank you so much most people are afriad of kernel writing


Thu Oct 08, 2009 8:21 pm
Post Re: How do i get my kernel to load binary programs?
error: Lvalue Required
Code:
SEGMENT(new_pointer) = SEGMENT(old_pointer) - (256 - OFFSET(old_pointer)) / 16


Thu Oct 08, 2009 8:46 pm

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
what a headache, that error is mostly likely caused because the tiny model uses simple word offsets as pointers... I would say a solution to that is to construct a far pointer in assembly and make a far call, but BASM won't let us do that...

more problems... when you compile using tlink /t /n stub+test, test.bin... its not generating a .COM file... the data from test.c originates as if the address of main() was the starting point but what we wanted was the starting point to be in stub.asm anyways that can be fixed by doing some fancy work in stub.asm and both problems can be fixed with one stone... the only thing would be to make sure the program is loaded in a paragraph aligned address...

:roll: I am so looking for to ending this sharade once and for all...

keep you posted...


Fri Oct 09, 2009 12:57 pm
Profile

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
update
what I said about the data originating from the start of main was not entirely true, what happens is that on the bin file all the code goes first... then the initialized data... then when its loaded on memory the DS points somewhere towards the end of where the file was loaded so that it has access to the initialized data, then the memory after is used for uninitialized data...

the problem, even if its compiled to a stand alone EXE it executes wrongly, the data is missaligned

bottom line
we need a manual of how to use TC without the standard library, hacking is getting us no-where!!!


Fri Oct 09, 2009 2:42 pm
Profile

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
update
never mind, I was using NASM incorrectly on a different PC went back to TASM on my pentium 75, the story changed, I think it'll work now... bad shenanigans!!!


Fri Oct 09, 2009 3:36 pm
Profile
Post Re: How do i get my kernel to load binary programs?
LOL


Fri Oct 09, 2009 4:56 pm

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
I did it!!!!
it got crazy but its done!!!

first is kernel.c, which is not a true kernel just a program that executes .COM files
Code:
#include <stdio.h>

// this is a psudo kernel that executes
// a simple custom .COM file

extern pascal int exec(void *prog);

char *buff;
char *prog;

//---------------------------------
void main(void) {
   FILE *f;
   int file_length;

   f = fopen("test.com", "rb");
   fseek(f, 0, SEEK_END);
   file_length = ftell(f);
   fseek(f, 0, SEEK_SET);
   buff = (char *) malloc(file_length + 16);
   if (buff == NULL) {
      printf("not enought memory\n");
      exit(0);
   }
   prog = buff + 16 - (((unsigned int) buff) % 16);
   fread(prog, 1, file_length, f);
   fclose(f);

   printf("\nLoading Program...\n");
   exec((void *) prog);
   printf("...Program returned safely to kernel\n");
}

kernel.c will need exec.asm to have more control over the pointer math and be able to make a far call
Code:
.MODEL TINY
.CODE
PUBLIC EXEC
EXEC PROC NEAR
   PUSH  bp
   MOV   bp, sp
   SUB   sp, 4
   PUSH  ds
   MOV   bx, [bp+4]
   SUB   bx, 256
   SHR   bx, 4
   MOV   ax, cs
   ADD   ax, bx
   MOV   ds, ax
   MOV   WORD PTR [bp - 4], 256
   MOV   WORD PTR [bp - 2], ax
   CALL  DWORD PTR [bp - 4]
   POP   ds
   ADD   sp, 4
   POP   bp
   RET 2
ENDP
END

next is test.c the test program with some data and a little DOS independent procedure
Code:
char *hello = "Hello World\xA\xD";

void PrintHello(void) {
   asm {
      MOV  ah, 0x0E
      MOV  bh, 0x00
      MOV  bl, 0x07
      MOV  si, hello
   }
   print_loop:
   asm {
      LODSB
      OR   al, al
      JZ   quit_loop:
      INT  0x10
      JMP  print_loop:
   }
   quit_loop:
}

void main(void) {
   PrintHello();
   return;
}

test.c will need stub.asm to make it go...
Code:
.MODEL TINY
.CODE
org 100h
.DATA
.DATA?
DGROUP GROUP _DATA,_BSS
.CODE
extrn _main:far
start:
   MOV  ax,cs
   MOV  ds,ax
assume  ds:DGROUP
   MOV  DGROUP@,ax
   CALL _main
   POP  ax  ; <- I don't like it this way but
            ;    TASM won't have it any other
            ;    fucking way. Yes I used FUCK
            ;    cause it was FUCKING CRAZY!!!
   RETF
public DGROUP@
DGROUP@ dw 0
END start

and last here its the batch file go.bat just to show how I compiled it
Code:
TASM stub.asm
TCC -c -mt -1 test.c
TLINK /t /n stub+test, test.com

TASM exec.asm
TCC -v -mt kernel.c exec.obj


Fri Oct 09, 2009 9:48 pm
Profile

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
special thanks go to Turbo Debugger... as I could not have done it without it... no freaking way... not after the stunts TASM was pulling...

I suppose you are going to implement INT calls the same way DOS does to provide OS services... cause I don't know how you can do using ordinary calls...


Fri Oct 09, 2009 9:57 pm
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 67 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6, 7  Next


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin and tweaked by the BF Team.