Page 1 of 1

Got some problems while running the kernel

Posted: Tue Feb 04, 2014 2:29 am
by sebastian93921
I'm just trying to build the os these few months
This is the kernel.c of my os and kstdio.c which provide the kprint functions

Code: Select all

#include "kernel.h"
#include "kstdio.h"
#include "io.h"

#define WHITE_TXT	0x0B 						//white on black text
#define GRAY_TXT    0x07						//gray on black text

#define SRC_HEIGHT 640
#define SRC_WIDTH 480

#define COM_DATE "31/01/2014"					//Compiling Date

int k_main(void)
{
	kclrscr();
	kprint("MMMMMMMMMMMMMYYYYYYYYYY Operating System\nProtected Mode process ...\n");
	kprint("First prototype - Sebastian Ko \n");
	kprint("\nDate : ");kprint(COM_DATE);kprint("\n");
	kprintc("\nNO \\t or value type \n",GRAY_TXT);
	
	kprint("Register checking ... ");
	if(reg() == 0)kprintc("OK\n",GRAY_TXT);
	else kprintc("Fail\n",GRAY_TXT);
	
	kprint("\n->--------->");
}

int reg()
{
	int no = 100;	//Test asm output
	__asm__ __volatile__("movl %0, %%ebx" :: "r" (no): "%ebx");	//add to ebx
	no = 0;
	__asm__ __volatile__("movl %%ebx, %0" :"=r"(no):: "%ebx");	//get back
	
	if(no == 100)return 0;
	
	return 1;
}

Code: Select all

#include "kstdio.h"
#include "kernel.h"

/*********************
	Printing Method
*********************/
unsigned int line = 0;							//Line in screen (Text)
unsigned int text_pos = 0;						//Text position


//Easiest print method
void kprint(char *message){kprintc(message,0x0B);};


void kprintc(char *message,unsigned int text_color)	//Print with change default color
{
	char *vidmem 	= (char *) 0xb8000;			//define video memory
	unsigned int i	= 0;						//define memory position

	i=((line*80*2)+(text_pos*2));				//Memory MUST be line * colume * TxtSpace (160 Means Over Screen of line,line++)	
	
	while(*message!=0)
	{
		if(line > CONSOLE_MAX_LINE)line = 0;
		
		//\n \r \t is 1 char
		if(*message == '\n') 					//Check for a new line
		{	
			text_pos = 0;						//reset text position
			line++;
			i=(line*80*2);
			message++;
		}else if(*message == '\r'){				//Return to head from line
			text_pos = 0;						//reset text position
			i = (line*80*2);
			message++;
		}else{									//print Message (Char)
			vidmem[i]=*message;
			message++;
			i++;
			vidmem[i]=text_color;
			i++;
			text_pos++;
		}
	}
};



void kclrscr() 									// clear the entire text screen
{
	char *vidmem = (char *) 0xb8000;
	unsigned int i=0;
	while(i < (80*CONSOLE_MAX_LINE*2))
	{
		vidmem[i]=' ';
		i++;
		vidmem[i]=0x00;
		i++;
	}
	line = 0;
};
but some string is not printed out like this:
Capture.JPG
The last line is "\n->--------->" but it can't show the whole string

Whats things I did wrong :( ?

Re: Got some problems while running the kernel

Posted: Tue Feb 04, 2014 3:32 am
by Combuster
Probably using CLI; HLT; in QEMU, because that prevents pending screen updates from happening.

Re: Got some problems while running the kernel

Posted: Tue Feb 04, 2014 4:34 am
by sebastian93921
Combuster wrote:Probably using CLI; HLT; in QEMU, because that prevents pending screen updates from happening.
can't :( I think there has a memory problems in the program
I have another problems too, when I using a linker "linker.ld"

Code: Select all

/*
 * Linker for bootloader access
 */
 
/*Default entry for bootloader to kernel*/
ENTRY(start)

/*File which needed to link together*/
INPUT
(
	loader.o
	kernel.o
	kstdio.o
	io.o
)


/* Tell where the various sections of the object files will be put in the final
   kernel image. */
SECTIONS
{
	/* Begin putting sections at 1 MiB, a conventional place for kernels to be
	   loaded at by the bootloader. */
	. = 0xEFFF;

	/* First put the multiboot header, as it is required to be put very early
	   early in the image or the bootloader won't recognize the file format.
	   Next we'll put the .text section. */
	.text : ALIGN(0x1000)
	{
		*(.text)
	}

	/* Read-only data. */
	.rodata :
	{
		*(.rodata)
	}

	/* Read-write data (initialized) */
	.data :
	{
		*(.data)
	}

	/* Read-write data (uninitialized) and stack */
	.bss :
	{
		*(COMMON)
		*(.bss)
		*(.bootstrap_stack)
	}

	/* The compiler may produce other sections, by default it will put them in
	   a segment with the same name. Simply add stuff here as needed. */
}
Some characters didn't printed out. :(
Capture.JPG

Re: Got some problems while running the kernel

Posted: Tue Feb 04, 2014 5:27 am
by Combuster
/* Begin putting sections at 1 MiB, a conventional place for kernels to be
loaded at by the bootloader. */
. = 0xEFFF;
-1 point for not following the tutorial. And judging from this, I fear there's a big broken hack hiding behind all the other instructions you didn't follow. Which begs the question: why?

Re: Got some problems while running the kernel

Posted: Tue Feb 04, 2014 5:52 am
by sebastian93921
Combuster wrote:
/* Begin putting sections at 1 MiB, a conventional place for kernels to be
loaded at by the bootloader. */
. = 0xEFFF;
-1 point for not following the tutorial. And judging from this, I fear there's a big broken hack hiding behind all the other instructions you didn't follow. Which begs the question: why?
i change to 0xEFFF because my boot file have to jump to position 0xEFFF to start 32bit
did I did somethings wrong in my bootloader?

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	File:	boot.asm
;	Title:	Sample bootloader
;	Desc:	Starting up Booting to kernel - Stage 1
;			containing load GDT and loading booting devices
;			
;			This is a i686(i386) 32bit loader			
;
;
;	Author:	Sebastian Ko
;	Date:	09/01/2011	v1.0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

[bits 16]				;asign  to 16bit CPU
[org 0x7c00]			;origin start point

jmp start

;Data
BootTitle db 'OS BootLoader x16 07122012 - Loading in Real Mode ...',13,10,0
GotoStage2 db 'Switching to Stage 2 ...',13,10,0

start:
	mov ax, 0x0000		;asignning a 0 address to ax for data segment address
	mov ds, ax			;move the address 0x0000 to data segment register
	
	mov si, BootTitle	;moving the String data to String interrupts register preper to print out
	call printString	;call print out method
	
	call reset_drive	;load kernel
	
	;;;;;;;;;;;;;;;;;;;;;
	;Goto Stage 2 file
	;;;;;;;;;;;;;;;;;;;;;
	mov si, GotoStage2	;moving the String data to String interrupts register preper to print out
	call printString	;call print out method
	
	cli
	lgdt [gdt_desc]		;Load GDT Descriptor
	
	jmp 0:0xEFFF		;code selector is 8 , goto kernel position EFFFh
	
	hlt					;Stop CPU moving

;-----------------------------------------------------------------------------------------------------
	
	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Startup Drive
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
reset_drive:			;reset floppy drive
	mov ah, 0x00		;
	int 0x13			;floppy drive interrupt
	or ah, ah			;if it's 0(return code)
	jnz reset_drive		;jump back
	
	mov ax, 0			;
	mov es,	ax			;reset extended segment
	mov bx,	0xEFFF		;set kernel location to 0000:EFFF(ES:BX)
	
	mov ah, 0x02		;commend - "read sector from disk"			
	mov al, 0x02		;Number of sectors need to read				
	mov ch, 0			;disk cylinder
	mov cl, 0x02		;set sector (0x01 is bootloader)			
	mov dh, 0			;disk head
	int 0x13			;call floppy drive interrupt
	or ah, ah			;check error
	jnz reset_drive
	
	ret


	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;String printing
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
printString:			;method (printString)
	pusha
	mov ah, 0x0E		;asignning 0x0E(video display) instruction to ah(accumulator register)
	mov bh, 0x00		;asignning page no (default is 0x00)
	mov bl, 0x07		;asignning text color (07 = White)
	
	;
	;FULL string display in 16bits
	;AX(AH+AL) 0x0E??	<-- adding to register, not yet execute (int)
	;			 | |-Means ACSII code (XY) X = bg color Y = Text Color (16bit)
	;			 |---Means Display  
	;
	;BX(BH+BL) 0x00??   <-- adding to register, means display usage
	;			 | |-Means front color (Can't display without no color
	;			 |---Means Page no (Default is page 0)
	;
	;int 0x10 <------------ interrupt address (execute)
	;				|------ 0x10 is display port address services
	;
		
	.getChar:
		;lodsb			;load a string block 'x''y''z' = "xyz" (LOaD String Block)
						;load a put it into AL register
		mov al,[si]		;get data from pointer of source index		;This method same as using lodsb
		inc si			;si check location + 1						;
		or al,al		;or gate (if-or)
		jz .return		;jump if the or gate returns 0
		
		int 0x10		;interrupt the Video Display instruction
		jmp .getChar	;loop if bytes not equals to 0
	
	.return:
	popa
	ret					;return method back to main



	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Set GDT
;If you write wrong any word
;means you will die for debugging
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	GDT:
;00000000 00000000 00000000 00000000 00000000 00000000	;GDT_Null
;11111111 00000000 00000000 10011010 11001111 00000000	;GDT_Code
;11111111 00000000 00000000 10010010 11001111 00000000	;GDT_Data

gdt: 		;making Global Description Table
			;first is null segment description
			;then is code segment
			;and the last is data segment

gdt_null: 			;Null Segment
	dd 0			;all 64bits 0
	dd 0			;
   
gdt_code: 			;Code Segment description
	dw 0FFFFh 		;16 bits
	dw 0 			;16 bits
	db 0 			;8 bits
	db 10011010b	;8 bits
	db 11001111b	;8 bits
	db 0 ;8 bits

gdt_data: 			;Data Segment description
	dw 0FFFFh
	dw 0
	db 0
	db 10010010b	;number 4 bit will change to 0 mean data segment description
	db 11001111b
	db 0

gdt_end:

gdt_desc:
	dw gdt_end - gdt - 1
	dd gdt

	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	
times 510-($-$$) db 0	;for boot loader size MUST be 512bytes
						;adding bytes with 0 to increase the size of file
	dw 0xAA55			;BOOT sign

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	File:	loader.asm
;	Desc:	File that using for preparing starting up OS.
;			Starting at bits 16 is setting UP Protected Mode
;			and enable A20.
;			Starting at bits 32 is checking for PMode
;			and change the memory section to Kernel(in C).
;	
;	Author:	Sebastian Ko
;	Data:	09/01/2012	v1.0
;
;	PS.	File (.asm) was written in Notepad++
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[bits 16]
[global start]

jmp start

start:	
	mov al, 'J'			;Jump to kernel
	call printChar
	mov al, 'u'
	call printChar
	mov al, 'm'
	call printChar
	mov al, 'p'
	call printChar
	mov al, ' '
	call printChar
	mov al, 't'
	call printChar
	mov al, 'o'
	call printChar
	mov al, ' '
	call printChar
	mov al, 'k'
	call printChar
	mov al, 'e'
	call printChar
	mov al, 'r'
	call printChar
	mov al, 'n'
	call printChar
	mov al, 'e'
	call printChar
	mov al, 'l'
	call printChar
	mov al, 13
	call printChar
	mov al, 10
	call printChar
	

	;;;;;;;;;;;;;;;;;;;;;;;
	;Switch PMode
	;;;;;;;;;;;;;;;;;;;;;;;
	mov eax, cr0		;move cr0(including protected mode instruction) to eax for checking
	or eax, 1			;checking al(2bits) 0001b
	mov cr0, eax		;giving back
	
	jmp 08:loadBit		;jump to 32bit kernel loader
	

	
;;;;;;;;;;;;;;;;;;;;;;
;Print Char
;;;;;;;;;;;;;;;;;;;;;;
printChar:
	mov ah, 0x0E		;instruction of 0x10 (display char)
	mov bx, 0x0007		;00 = page  07 = black color bg and white text
	int 0x10			;interrupt display code
	ret
	
	
;================================================================================
;================================================================================

[bits 32]
[section .text]
[extern k_main]
loadBit:
	mov ax, 10h					;reset segment to 10h (data selector)
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	mov ss, ax
	
	mov word [es: 0xb8000],'P'	;Protected Mode
	mov word [es: 0xb8001],02h	
	mov word [es: 0xb8002],'M'
	mov word [es: 0xb8003],02h
	mov word [es: 0xb8004],'o'
	mov word [es: 0xb8005],02h
	mov word [es: 0xb8006],'d'
	mov word [es: 0xb8007],02h
	mov word [es: 0xb8008],'e'
	mov word [es: 0xb8009],02h
	
	;mov esp, 0x90000			;stack begins from 90000h
	
	call k_main
	
	hlt
;================================================================================
;================================================================================