What's wrong with my kernel or other other files or how I'm ru

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

53 Replies - 1363 Views - Last Post: 06 February 2018 - 12:34 PM Rate Topic: -----

#1 Programmingisnotmything  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 18-September 14

What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 01:12 PM

I've always wondered what it would be like to write my own Kernel and beyond. I'm just a Network Admin major with a minor in programming though and I just recently finished writing the sample code in the Modern Assembly book by Daniel Kussworm, which includes the C++ and Assembly sample code for whatever purpose. I wrote the sample code to help me become familiar with writing Assembly optimized C++ programs and read the book, but even though I know how to run both the Assembly and C++ I still don't understand completely what the code is doing yet if I ever will. Also, self teaching yourself the rest of C++ from Tony Gaddis's book is tough and so is doing the same for Assembly optimized C++ programs in Daniel Kussworms books too. You could say this effort might be to ambitious as well.

Anyway I found this guide I how to write a simple sample Kernel here:

http://arjunsreedhar...-write-a-kernel

After successfully debugging the code for all the files needed, like the kernel.asm, kernel.c, and link.ld I finally got it to the kernel to display "My First Kernel" on boot or in terminal by using qemu on my laptop and after making flowcharts even though I need to link the shapes regardless of how weird LibreDraw makes it because the pages of the flow chart aren't on an infinite scrolling document page, like Visio. However, now I'm trying to get the kernel2.c to compile using the kernel 201 guide and a little tweeting, but I can't get rid of these errors in the kernel2.c regardless if I got kernel2.asm to compile without error. The biggest problem I have is that this is not all my own code and the author of kernel 201 the same author of kernel 101 doesn't exactly tell you where to put the code contained in the kernel 201 guide. Therefore I get the following most recent errors:

[email protected]:~$ gcc -m32 -lm -c /home/username/Documents/My\ first\ sample\ kernel/kernel2.c -o /home/username/Documents/My\ first\ sample\ kernel/kc2.o
/home/username/Documents/My first sample kernel/kernel2.c:19:1: warning: parameter names (without types) in function declaration
void keyboard_handler_main(int write_port, int read_port(KEYBOARD_DATA_PORT), i
^
/home/username/Documents/My first sample kernel/kernel2.c:19:1: warning: parameter names (without types) in function declaration
/home/username/Documents/My first sample kernel/kernel2.c:19:83: error: redefinition of parameter ‘read_port’
handler_main(int write_port, int read_port(KEYBOARD_DATA_PORT), int read_port(K
^
/home/username/Documents/My first sample kernel/kernel2.c:19:48: note: previous definition of ‘read_port’ was here
void keyboard_handler_main(int write_port, int read_port(KEYBOARD_DATA_PORT), i
^
/home/username/Documents/My first sample kernel/kernel2.c:19:134: error: ‘keycode’ undeclared here (not in a function)
_DATA_PORT), int read_port(KEYBOARD_STATUS_PORT) , int keyboard_map[keycode]); 
^
/home/username/Documents/My first sample kernel/kernel2.c:20:13: error: ‘current_loc’ undeclared here (not in a function)
char vidptr[current_loc++] = keyboard_map[keycode]; // Pointer to a char
^
/home/username/Documents/My first sample kernel/kernel2.c:20:30: error: ‘keyboard_map’ undeclared here (not in a function)
char vidptr[current_loc++] = keyboard_map[keycode]; // Pointer to a char
^
/home/username/Documents/My first sample kernel/kernel2.c:20:43: error: ‘keycode’ undeclared here (not in a function)
char vidptr[current_loc++] = keyboard_map[keycode]; // Pointer to a char
^
/home/username/Documents/My first sample kernel/kernel2.c: In function ‘kmain’:
/home/username/Documents/My first sample kernel/kernel2.c:26:2: error: unknown type name ‘IDT_entry’
IDT_entry IDT[IDT_SIZE]; // IDT is an IDT_entry structure.
^
/home/username/Documents/My first sample kernel/kernel2.c:26:16: error: ‘IDT_SIZE’ undeclared (first use in this function)
IDT_entry IDT[IDT_SIZE]; // IDT is an IDT_entry structure.
^
/home/username/Documents/My first sample kernel/kernel2.c:26:16: note: each undeclared identifier is reported only once for each function it appears in
/home/username/Documents/My first sample kernel/kernel2.c:30:18: error: ‘write_port’ undeclared (first use in this function)
idt_init(write_port, load_idt, IDT);
^
/home/username/Documents/My first sample kernel/kernel2.c:30:30: error: ‘load_idt’ undeclared (first use in this function)
idt_init(write_port, load_idt, IDT);
^
/home/username/Documents/My first sample kernel/kernel2.c:32:9: warning: parameter names (without types) in function declaration
int keyboard_handler_main(write_port, read_port, keyboard_map);
^
/home/username/Documents/My first sample kernel/kernel2.c:32:13: error: conflicting types for ‘keyboard_handler_main’
int keyboard_handler_main(write_port, read_port, keyboard_map);
^
/home/username/Documents/My first sample kernel/kernel2.c:19:6: note: previous declaration of ‘keyboard_handler_main’ was here
void keyboard_handler_main(int write_port, int read_port(KEYBOARD_DATA_PORT), i
^
/home/username/Documents/My first sample kernel/kernel2.c: At top level:
/home/username/Documents/My first sample kernel/kernel2.c:60:6: error: conflicting types for ‘idt_init’
void idt_init(void)
^
/home/username/Documents/My first sample kernel/kernel2.c:17:6: note: previous declaration of ‘idt_init’ was here
void idt_init(int write_port, int load_idt, int IDT[]); // function prototype
^
/home/username/Documents/My first sample kernel/kernel2.c: In function ‘idt_init’:
/home/username/Documents/My first sample kernel/kernel2.c:67:36: error: ‘keyboard_handler’ undeclared (first use in this function)
keyboard_address = (unsigned long)keyboard_handler;
^
/home/username/Documents/My first sample kernel/kernel2.c:68:2: error: ‘IDT’ undeclared (first use in this function)
IDT[0x21].offset_lowerbits = keyboard_address & 0xffff;
^
/home/username/Documents/My first sample kernel/kernel2.c:82:2: warning: implicit declaration of function ‘write_port’ [-Wimplicit-function-declaration]
write_port(0x20 , 0x11);
^
/home/username/Documents/My first sample kernel/kernel2.c:110:44: error: ‘IDT_SIZE’ undeclared (first use in this function)
idt_ptr[0] = (sizeof (struct IDT_entry) * IDT_SIZE) +
^
/home/username/Documents/My first sample kernel/kernel2.c:114:2: warning: implicit declaration of function ‘load_idt’ [-Wimplicit-function-declaration]
load_idt(idt_ptr);
^
/home/username/Documents/My first sample kernel/kernel2.c: At top level:
/home/username/Documents/My first sample kernel/kernel2.c:117:6: error: conflicting types for ‘kb_init’
void kb_init(void) 
^
/home/username/Documents/My first sample kernel/kernel2.c:18:6: note: previous declaration of ‘kb_init’ was here
void kb_init(int write_port); // function prototype
^
/home/username/Documents/My first sample kernel/kernel2.c:123:6: error: conflicting types for ‘keyboard_handler_main’
void keyboard_handler_main(void) {
^
/home/username/Documents/My first sample kernel/kernel2.c:32:13: note: previous declaration of ‘keyboard_handler_main’ was here
int keyboard_handler_main(write_port, read_port, keyboard_map);
^
/home/username/Documents/My first sample kernel/kernel2.c: In function ‘keyboard_handler_main’:
/home/username/Documents/My first sample kernel/kernel2.c:131:11: warning: implicit declaration of function ‘read_port’ [-Wimplicit-function-declaration]
status = read_port(KEYBOARD_STATUS_PORT);
^
/home/username/Documents/My first sample kernel/kernel2.c:131:21: error: ‘KEYBOARD_STATUS_PORT’ undeclared (first use in this function)
status = read_port(KEYBOARD_STATUS_PORT);
^
/home/username/Documents/My first sample kernel/kernel2.c:135:38: error: ‘KEYBOARD_DATA_PORT’ undeclared (first use in this function)
keycode = read_port(KEYBOARD_DATA_PORT);
^
[email protected]:~$ 



Here's my code though if anyone can help and yes I know the error messages are pretty straight forward, but I've never written a kernel before this or even this advanced regardless if this isn't even that advanced yet and everything I've tried hasn't worked. Also, I don't know what I'm missing or haven't tried either, so I need help figuring it out because I don't know what elso to try. Also, I only took one C/C++ programming class and we only learned up to Array's I think, even if I completed the rest of the book by Tony Gaddis up to binaries because I didn't have any help in explaining what the rest of C or C++ does and still haven't found the answers. I know binaries are used in Linux though for C Make installation files if I'm not mistaken, but I haven't written one yet and I'm doing everything I can including versioning:

/*
* kernel.c
*/

#include <stdio.h>
#include <math.h>

struct IDT_entry
{
        unsigned short int offset_lowerbits;
        unsigned short int selector;
        unsigned char zero;
        unsigned char type_attr;
        unsigned short int offset_higherbits;
};

void idt_init(int write_port, int load_idt, int IDT[]); // function prototype
void kb_init(int write_port); // function prototype
void keyboard_handler_main(int write_port, int read_port(KEYBOARD_DATA_PORT), int read_port(KEYBOARD_STATUS_PORT) , int keyboard_map[keycode]); // function prototype
char vidptr[current_loc++] = keyboard_map[keycode]; // Pointer to a char
unsigned long idt_ptr[2]; // Pointer to a unsigned long


void kmain(void)
{
	IDT_entry IDT[IDT_SIZE]; // IDT is an IDT_entry structure.

	const char *str = "my first kernel";
	char *vidptr =(char*)0xb8000; //video mem begins here.
        idt_init(write_port, load_idt, IDT);
        kb_init(write_port);
        int keyboard_handler_main(write_port, read_port, keyboard_map);
	unsigned int i = 0;
	unsigned int j = 0;

	/* this loop clears the screen
	* there are 25 lines each of 80 columns; each element takes 2 bytes */
	while(j < 80 * 25 * 2) {
	     /* blank character */
             vidptr[j] = ' ';
	     /* attribute-byte - light grey on black screen */
	     vidptr[j+1] = 0x07;
             j = j + 2;
        }

        j = 0;

        /* this loop writes the string to video memory */
        while(str[j] != '\0') {
             /* the character's ascii */
             vidptr[i] = str[j];
             /* attribute-byte: give character black bg light grey fg */
	     vidptr[i+1] = 0x07;
	     ++j;
	     i = i + 2;
       }
       return;
} 

void idt_init(void)
{
	unsigned long keyboard_address;
	unsigned long idt_address;
	unsigned long idt_ptr[2];
	
	/* populate IDT entry of keyboard's interrupt */
	keyboard_address = (unsigned long)keyboard_handler;
	IDT[0x21].offset_lowerbits = keyboard_address & 0xffff;
	IDT[0x21].selector = 0x08; /* KERNEL_CODE_SEGMENT_OFFSET
*/
	IDT[0x21].zero = 0;
	IDT[0x21].type_attr = 0x8e; /* INTERRUPT_GATE */
	IDT[0x21].offset_higherbits = (keyboard_address &
	0xffff0000) >> 16;
	/* 	Ports
	*	PIC1 PIC2
	*Command 0x20 0xA0
	*Data 0x21 0xA1
*/

	/* ICW1 - begin initialization */
	write_port(0x20 , 0x11);
	write_port(0xA0 , 0x11);

	/* ICW2 - remap offset address of IDT */
	/*
	* In x86 protected mode, we have to remap the PICs beyond
0x20 because
	* Intel have designated the first 32 interrupts as
"reserved" for cpu exceptions
*/
	write_port(0x21 , 0x20);
	write_port(0xA1 , 0x28);

	/* ICW3 - setup cascading */
	write_port(0x21 , 0x00);
	write_port(0xA1 , 0x00);

	/* ICW4 - environment info */
	write_port(0x21 , 0x01);
	write_port(0xA1 , 0x01);
	/* Initialization finished */

	/* mask interrupts */
	write_port(0x21 , 0xff);
	write_port(0xA1 , 0xff);

	/* fill the IDT descriptor */
	idt_address = (unsigned long)IDT ;
	idt_ptr[0] = (sizeof (struct IDT_entry) * IDT_SIZE) +
((idt_address & 0xffff) << 16);

	idt_ptr[1] = idt_address >> 16 ;
	load_idt(idt_ptr);
}

void kb_init(void) 
{
	/* 0xFD is 11111101 - enables only IRQ1 (keyboard)*/
	write_port(0x21 , 0xFD);
}

void keyboard_handler_main(void) {
         unsigned char status;
         char keycode;
         char vidptr[current_loc++] = keyboard_map[keycode]; // Pointer to a char
 
         /* write EOI */
         write_port(0x20, 0x20);
 
 status = read_port(KEYBOARD_STATUS_PORT);
         /* Lowest bit of status will be set if buffer is not
 52 empty */
         if (status & 0x01) {
                 keycode = read_port(KEYBOARD_DATA_PORT);
                 if(keycode < 0) 
                         return;
                 vidptr[current_loc++] = keyboard_map[keycode];
                 vidptr[current_loc++] = 0x07;

       }
       return;
}



The one screensshot for kernel 101 is attached below, but kernel 101 works.

Is This A Good Question/Topic? 0
  • +

Replies To: What's wrong with my kernel or other other files or how I'm ru

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13747
  • View blog
  • Posts: 54,900
  • Joined: 12-June 08

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 01:22 PM

Per my recommendations on the hardforum thread - parameters need to be unique names, and you are - most likely - missing a #define.
Was This Post Helpful? 0
  • +
  • -

#3 Programmingisnotmything  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 18-September 14

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 01:25 PM

Where am I missing it and what do I need to put because I can't find a good example or explanation anywhere and I've tried stackexchange and everywhere else?

This post has been edited by Skydiver: 18 January 2018 - 06:05 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13747
  • View blog
  • Posts: 54,900
  • Joined: 12-June 08

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 01:29 PM

If you are not sure why parameters need to be unique you should step back and take a look at some basic C and c++ books and work through them.

Then hit up the github for the site you are looking at and try running it fresh and clean after pulling down the code.
https://github.com/arjun024/mkernel

I don't know where you found parts of that other code and mashed it in there, but go back to the source and review what you are trying to shoe horn in.
Was This Post Helpful? 0
  • +
  • -

#5 Programmingisnotmything  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 18-September 14

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 01:46 PM

Thanks I'm using Tony Gaddis's book too as well as stackexchange and other places, but I don't think I've tried github though. I know parameters need to be unique, but I don't remember why though and can only say that they must or else I get errors like this. Maybe I do need to go back through some basic C++ books though.

This post has been edited by Skydiver: 18 January 2018 - 06:05 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13747
  • View blog
  • Posts: 54,900
  • Joined: 12-June 08

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 01:47 PM

You should.. This is certainly an interesting project but best put a few weeks of foundation layering then you can come back to this and make more sense out of it.
Was This Post Helpful? 0
  • +
  • -

#7 Programmingisnotmything  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 18-September 14

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 01:53 PM

Yea and I need to update my flowcharts or make new ones for this code too.

This post has been edited by Skydiver: 18 January 2018 - 06:04 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13747
  • View blog
  • Posts: 54,900
  • Joined: 12-June 08

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 01:54 PM

The joys never end, amirite?
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1437
  • View blog
  • Posts: 4,621
  • Joined: 20-March 10

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 02:01 PM

Work through the errors one by one....

the first one is write_port is undefined...

so write_port seems to be in kernel.asm

so something like

extern void write_port(unsigned short port, unsigned char data);


would be needed in the C file, before you can use it.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6007
  • View blog
  • Posts: 20,641
  • Joined: 05-May 12

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 06:03 PM

Programmingisnotmything: There is no need to quote the post directly above yours. Just use the big Reply button or the Fast reply area.
Was This Post Helpful? 0
  • +
  • -

#11 Programmingisnotmything  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 18-September 14

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 07:44 PM

View Postmodi123_1, on 18 January 2018 - 01:54 PM, said:

The joys never end, amirite?


I did only minor in programming though as well as only learned Visual Basic.Net, Java, C++, and HTML along with CSS3, Javascript and XML. I just recently finished the book on Assembly by Daniel Kussworm too, but I can't make excuses. However, there is either something I didn't learn or need to improve on and maybe it's System Analysis and Design, which I didn't take.
Was This Post Helpful? 0
  • +
  • -

#12 Programmingisnotmything  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 18-September 14

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 08:01 PM

View Postsnoopy11, on 18 January 2018 - 02:01 PM, said:

Work through the errors one by one....

the first one is write_port is undefined...

so write_port seems to be in kernel.asm

so something like

extern void write_port(unsigned short port, unsigned char data);


would be needed in the C file, before you can use it.


Thanks I did that, but I couldn't figure out what my code was missing when following the authors kernel 201 guide. I used Linux, the vim text editor, nasm, and gcc to compile all the file to follow the guide for both kernel 101 and 201 though.

Here's how the kernel2.asm references the assembly code you stated would point to the c code:

write_port:
	mov edx, [esp + 4]
	mov al, [esp + 4 + 4]
	out dx, al
	ret


and the rest of the code though, which is completely missing what you suggest thogh. However it compiled correctly, so this probably isn't related to that and this section of this forum is not about Assembly programming, but maybe that missing piece of the code you suggested is the problem. Therefore, if we want to discuss Assembly language programming I probably have to open a thread under that section:

;;kernel.asm
bits 32				;nasm directive - 32-bit
section .text
	;multiboot spec
	align 4
	dd 0x1BADB002		;magic
	dd 0x00			;flags
	dd - (0x1BADB002 + 0x00) ;checksum. m+f+c should be zero

global start
extern kmain			;kmain is defined in the c file
extern keyboard_handler_main	;keyboard_handler_main is defined in the c file

start:
  cli                           ;block interrupts
  mov esp, stack_space		;set stack pointer
  call kmain
  hlt:				;halt the CPU
  
read_port:
	mov edx, [esp + 4]
	in al, dx
	ret
write_port:
	mov edx, [esp + 4]
	mov al, [esp + 4 + 4]
	out dx, al
	ret

load_idt:
	call idt_init
	mov edx, [esp + 4]
	lidt [edx]
	sti
	ret

idt_init:
	call idt_init
	ret

kb_init:
        call kb_init
        ret

keyboard_handler:
	call keyboard_handler_main
	iretd

section .bss
resb 8192			;8kb for stack
stack_space: 

Was This Post Helpful? 0
  • +
  • -

#13 Programmingisnotmything  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 18-September 14

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 18 January 2018 - 08:25 PM

View Postsnoopy11, on 18 January 2018 - 02:01 PM, said:

Work through the errors one by one....

the first one is write_port is undefined...

so write_port seems to be in kernel.asm

so something like

extern void write_port(unsigned short port, unsigned char data);


would be needed in the C file, before you can use it.


I did this but got redefined error and identifier expected when I add this, sh their there I must not need it or I'm doing something wrong.
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1437
  • View blog
  • Posts: 4,621
  • Joined: 20-March 10

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 19 January 2018 - 03:13 PM

Perhaps this tutorial will help you understand better...


http://www.dreaminco...0&#entry2348765
Was This Post Helpful? 0
  • +
  • -

#15 Programmingisnotmything  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 18-September 14

Re: What's wrong with my kernel or other other files or how I'm ru

Posted 20 January 2018 - 11:35 AM

Thanks. Ok now I see after reading that and referring back to the reply the following came from that the following goes in the kernel.c or c file and not the kernel.asm or assembly file.

extern void write_port(unsigned short port, unsigned char data);


This post has been edited by Skydiver: 20 January 2018 - 05:07 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »