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

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

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

#46 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 05 February 2018 - 08:10 AM

View Postsnoopy11, on 04 February 2018 - 06:32 PM, said:

I never suggested anything of the sort,


look at what parameters do you actually use inside the function and what parameters do you actually return ?

And you're prototype is still different from its implementation..
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

  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;

}




[qoute]
I will take a guess at what I think you mean as I am still not convinced that you know what you mean by this mess and perhaps you will gain some insight into self...

int keyboard_handler_main(int current_loc, unsigned char status,char keycode, char* vidptr );

[/qoute]

Ok I have it like that now

[qoute]
this is your prototype or function defintion.

int keyboard_handler_main(int current_loc, unsigned char status,char keycode, char* vidptr )
{

vidptr[current_loc++] = keyboard_map[keycode]; /*note keyboard_map[] would have to be some global array not the best way of doing things perhaps use a function ?*/




  /* write EOI */
[/qoute]

Ok well I can't find a good explanation in Gaddis's book, but do you mean like this:

[code]char vidptr[current_loc++], keyboard_map[keycode]; // Pointer to a char


Quote

write_port(0x20, 0x20);/*write_port is an assembler routine but it needs to be defined as extern in the C file*/


It's right here:

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


Quote

status = read_port(KEYBOARD_STATUS_PORT);/* read_port is practically the same situation as write_port but this time it requires a #define in KEYBOARD_STATUS_PORT */


It's right here:

extern void read_port(unsigned short port);


Quote

/* Lowest bit of status will be set if buffer is not

empty */

/* Lowest bit of status will be set if buffer is not

empty */
if (status & 0x01) {

keycode = read_port(KEYBOARD_DATA_PORT);

if(keycode < 0)

return -1;/* return -1 for error condition*/

vidptr[current_loc++] = keyboard_map[keycode];

vidptr[current_loc++] = 0x07;



}

return current_loc; /* return the current location */

}


Ok I'll do this


[/code]

this would be your implementation...

This is still a mess but at least its a self consistent mess..

What you had is just illogical on any level and you don't seem to understand the basics of syntax or functions so how are you going to write a kernel ?

Ask yourself is this really what you want to do... as right now the aptitude in the level you need to be at in writing a kernel is simply not there.

Was This Post Helpful? -2
  • +
  • -

#47 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 05 February 2018 - 12:31 PM

I finally got it work by creating the following files: keyboard_map.h, gdt.c, idt.c, idt.h, interruptStubs.asm, interrupts.c, interrupts.h, io.asm, io.c, io.h, lib.c, pic.c, pic.h, pit.c, pit.h, ports.h, registers.h, thread.c, thread.h, threadS.asm, and vmem.c as well using the following code:

The code for kernel2.c

/*
* My First Kernel Version 2 With Keyboard Support by D2
* License: GPL version 2 or higher http://www.gnu.org/licenses/gpl.html
*/
#include "keyboard_map.h"

/* there are 25 lines each of 80 columns; each element takes 2 bytes */
#define LINES 25
#define COLUMNS_IN_LINE 80
#define BYTES_FOR_EACH_ELEMENT 2
#define SCREENSIZE BYTES_FOR_EACH_ELEMENT * COLUMNS_IN_LINE * LINES

#define KEYBOARD_DATA_PORT 0x60
#define KEYBOARD_STATUS_PORT 0x64
#define IDT_SIZE 256
#define INTERRUPT_GATE 0x8e
#define KERNEL_CODE_SEGMENT_OFFSET 0x08

#define ENTER_KEY_CODE 0x1C

extern unsigned char keyboard_map[128];
extern void keyboard_handler(void);
extern char read_port(unsigned short port);
extern void write_port(unsigned short port, unsigned char data);
extern void load_idt(unsigned long *idt_ptr);

/* current cursor location */
unsigned int current_loc = 0;
/* video memory begins at address 0xb8000 */
char *vidptr = (char*)0xb8000;

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

struct IDT_entry IDT[IDT_SIZE];


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 = KERNEL_CODE_SEGMENT_OFFSET;
	IDT[0x21].zero = 0;
	IDT[0x21].type_attr = 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 kprint(const char *str)
{
	unsigned int i = 0;
	while (str[i] != '\0') {
		vidptr[current_loc++] = str[i++];
		vidptr[current_loc++] = 0x07;
	}
}

void kprint_newline(void)
{
	unsigned int line_size = BYTES_FOR_EACH_ELEMENT * COLUMNS_IN_LINE;
	current_loc = current_loc + (line_size - current_loc % (line_size));
}

void clear_screen(void)
{
	unsigned int i = 0;
	while (i < SCREENSIZE) {
		vidptr[i++] = ' ';
		vidptr[i++] = 0x07;
	}
}

void keyboard_handler_main(void)
{
	unsigned char status;
	char keycode;

	/* write EOI */
	write_port(0x20, 0x20);

	status = read_port(KEYBOARD_STATUS_PORT);
	/* Lowest bit of status will be set if buffer is not empty */
	if (status & 0x01) {
		keycode = read_port(KEYBOARD_DATA_PORT);
		if(keycode < 0)
			return;

		if(keycode == ENTER_KEY_CODE) {
			kprint_newline();
			return;
		}

		vidptr[current_loc++] = keyboard_map[(unsigned char) keycode];
		vidptr[current_loc++] = 0x07;
	}
}

void kmain(void)
{
	const char *str = "my first kernel with keyboard support";
	clear_screen();
	kprint(str);
	kprint_newline();
	kprint_newline();

	idt_init();
	kb_init();

	while(1);
}


and the code for kernel2.asm:

;;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
global keyboard_handler
global read_port
global write_port
global load_idt

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]	;al is the lower 8 bits of eax
				;dx is the lower 16 bits of edx
	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			;turn on inturrupts
	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
  • +
  • -

#48 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 05 February 2018 - 12:41 PM

Now to fix this mess in kernel3.asm to bring my latest attempt at a kernel up to a shell, which is a discussion for the assembly section of this forum:

; My first kernel Version 3 with shell support
; License: GPL version 2 or higher http://www.gnu.org/licenses/gpl.html
; NASM syntax
bits 32
section .text
        ;multiboot spec
        align 4
        dd 0x1BADB002              ;magic
        dd 0x00                    ;flags
        dd - (0x1BADB002 + 0x00)   ;checksum. mfc should be zero

global start
global gdt_fush
global stack_space
global jump_usermode
global enter_usermode
global keyboard_handler
global read_port
global write_port
global load_idt

extern kmain 		;this is defined in the c file
extern run_kshell
extern keyboard_handler_main	;keyboard_handler_main is defined in the c file

global loadPageDirectory
loadPageDirectory:
    push ebp
    mov ebp, esp
    mov eax, [esp + 8]
    mov cr3, eax
    mov esp, ebp
    pop ebp
    ret

global enablePaging
enablePaging:
    push ebp
    mov ebp, esp
    mov eax, cr0
    or eax, 0x80000000
    mov cr0, eax
    mov esp, ebp
    pop ebp
    ret	

; This will set up our new segment registers. We need to do
; something special in order to set CS. We do what is called a
; far jump. A jump that includes a segment as well as an offset.
; This is declared in C as 'extern void gdt_flush();'
global gdt_flush     ; Allows the C code to link to this
extern gp            ; Says that '_gp' is in another file
gdt_flush:
    lgdt [gp]        ; Load the GDT with our '_gp' which is a special pointer
    mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:flush2   ; 0x08 is the offset to our code segment: Far jump!
flush2:
    ret               ; Returns back to the C code!

global tss_flush   ; Allows our C code to call tss_flush().
tss_flush:
   mov ax, 0x2B      ; Load the index of our TSS structure - The index is
                     ; 0x28, as it is the 5th selector and each is 8 bytes
                     ; long, but we set the bottom two bits (making 0x2B)
                     ; so that it has an RPL of 3, not zero.
   ltr ax            ; Load 0x2B into the task state register.
   ret   
    
    
 
global jump_usermode ;you may need to remove this _ to work right.. 
jump_usermode:
extern run_kshell
     mov ax,0x23
     mov ds,ax
     mov es,ax 
     mov fs,ax 
     mov gs,ax ;we don't need to worry about SS. it's handled by iret
 
     mov eax,esp
     push 0x23 ;user data segment with bottom 2 bits set for ring 3
     push eax ;push our current stack just for the heck of it
     pushf
     push 0x1B ;user code segment with bottom 2 bits set for ring 3
     call run_kshell ;may need to remove the _ for this to work right 
     iret
 

global enter_usermode
enter_usermode:
    cli
    mov ax, 0x23	; user mode data selector is 0x20 (GDT entry 3). Also sets RPL to 3
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
; Now we can perform the switch to user mode. This is done by building the stack frame for IRET and issuing the IRET:
    push 0x23		; SS, notice it uses same selector as above
    push esp		; ESP
    pushfd			; EFLAGS
    push 0x1b		; CS, user mode code selector is 0x18. With RPL 3 this is 0x1b
    lea eax, [a]		; EIP first
    push eax

    call run_kshell
    iretd
a:
    add esp, 4 ; fix stack
    rts:
 
 
start:
	cli 				;block interrupts
	mov esp, stack_space
	push ebx
	call kmain
	hlt 				;halt the CPU

read_port:
	mov edx, [esp + 4]	;al is the lower 8 bits of eax
				;dx is the lower 16 bits of edx
	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			;turn on inturrupts
	ret

idt_init:
	call idt_init
	ret

kb_init:
        call kb_init
        ret

keyboard_handler:
	call keyboard_handler_main
	iretd

section .bss
resb 32768; 8KB for stack
global stack_space
stack_space:


Regardless though if this is the right section here are the errors anyway for the kernel3.asm just for the sake of showing you where I'm at now and what errors I'm getting when trying to compile the kasm3.o and kc3.o into kernel3 using link.ld:

[email protected]:~$ ld -m elf_i386 -T /home/username/Documents/My\ first\ sample\ kernel/link.ld -o /home/username/Documents/My\ first\ sample\ kernel/kernel3 /home/username/Documents/My\ first\ sample\ kernel/kasm3.o /home/username/Documents/My\ first\ sample\ kernel/kc3.o
/home/username/Documents/My first sample kernel/kasm3.o: In function `gdt_flush':
/home/username/Documents/My first sample kernel/kernel3.asm:(.text+0x2f): undefined reference to `gp'
/home/username/Documents/My first sample kernel/kasm3.o: In function `jump_usermode':
/home/username/Documents/My first sample kernel/kernel3.asm:(.text+0x66): undefined reference to `run_kshell'
/home/username/Documents/My first sample kernel/kasm3.o: In function `enter_usermode':
/home/username/Documents/My first sample kernel/kernel3.asm:(.text+0x86): undefined reference to `run_kshell'
[email protected]:~$

Was This Post Helpful? 0
  • +
  • -

#49 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

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

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

Posted 05 February 2018 - 09:26 PM

Ok,

so why do you have extern in an asm file ?

extern is a C command ??

But I'm glad you are finally making some progress at least.
Was This Post Helpful? 0
  • +
  • -

#50 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 06 February 2018 - 01:22 AM

extern is an assembly command too look it up.

my first kernel and second have extern used in them like so:

extern kmain ;kmain is defined in the c file

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

Was This Post Helpful? 0
  • +
  • -

#51 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6010
  • View blog
  • Posts: 20,667
  • Joined: 05-May 12

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

Posted 06 February 2018 - 04:59 AM

Programmingisnotmything: As I said, do not quote the post above yours. Just use the big Reply button or the Fast Reply area. If you must quote, limit it to the specific point that you want to address. If you want to let somebody know that you are addressing them, use the member icon on the toolbar. It's the one that looks like a silhouette with a question mark. Doing a full quote does not help with the clarity of the thread, unless you are trying to quote something that is several pages back already.

Anyway, for us old school folks who learned assembly before 1996 and NASM didn't exist, the assembler directive was EXTRN for x86 based assemblers, and if I recall correctly XTERN for 6502 assembly. I can see why snoopy11 mistook the extern as a C keyword rather than an assembler directive.
Was This Post Helpful? 0
  • +
  • -

#52 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 06 February 2018 - 11:27 AM

*** removed quote ***

You're a jerk and you made and are making replying to your response or moderation of this thread or anything a pain instead of easy because instead of just allowing me to click qoute reply you made click reply or in the fast reply area, click the member icon and have to copy as well as paste your username or member name or someone else's, and then copy and paste what you said or what someone else said as well as put in qoutes when I just could have and previously have just been using or click qoute reply or multi-qoute and the qoute reply, which I don't know why this forum even has if I can't just do that instead of all this other non-sense you what me to do to save screen realstate and everything other excuses as to why you won't let me just do what every other normal forum lets me do.

As for extern as a command in Assembly though I knew this from reading and writing all the sample programs in Daniel Kussworm's book. As I didn't know or learn assembly back in 1996 because my family just got an Intel-based 286 from my uncle and the very next year an IBM Aptiva based on an AMD K6 233 MHz, which the main reason I didn't learn assembly is due to not even knowing how important it was or anything about it. Heck I was just finding out about C and C++, but I also thought learning COBOL to help solve the Y2k bug was important and thank goodness I didn't waste my time to learn COBOL though and all its flaws because it's not nearly as useful as C, C++, and Assembly or maybe even JAVA, which I've heard from Linus Torvalds that Java is a horrible programming language and was incredibly slow when they wrote an Operating System using it. I also never learned these languages sooner because I didn't have books and any way of learning them effectively sooner because everyone said I need Visual Studio and were butt buddies with Microsoft, so they didn't know crap about open source and Linux until much later and still offered no useful advice or recommendation on how to learn Linux or these programming languages. Heck, even I was kinda stubborn about ditching Microsoft because I didn't realize how much more freedom and everything else I'd get from using open source and Linux as well as freedom-respecting software. Instead of paying a ridiculously high premium or royalty's to use Microsoft product's and all the hassle the hassle that goes along with using Microsoft.

I've been blinded for so many years by Microsoft's prison over me and other people until I finally got the chance to learn how to use Linux properly in 2010 and all it's benefit's. Thank goodness I don't use Apple product's regularly either and only own a Mac Pro 2012, which when it dies I'll probably never buy anything Apple again because of all the hassle of being confined to Apple hardware just to easily use Apple's Operating System and let Apple dictate what I can do with my hardware.

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

Was This Post Helpful? 0
  • +
  • -

#53 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13765
  • View blog
  • Posts: 54,941
  • Joined: 12-June 08

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

Posted 06 February 2018 - 12:22 PM

Thumbs up for the lot of romanticism. :^:
Was This Post Helpful? 0
  • +
  • -

#54 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6010
  • View blog
  • Posts: 20,667
  • Joined: 05-May 12

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

Posted 06 February 2018 - 12:34 PM

Programmingisnotmything: Every forum has is own conventions and etiquette, much like any other human organization. You don't show up at the court house in overalls you wore while changing the oil in your car. Many societies still appreciate the use of "please" and "thank you". In some societies, not standing for that countries national anthem is grounds for harsh punishment. I'm just trying to tell you what the culture is for this particular forum. In another forum I participate in which uses this same (antiquated) forum software, their convention is to fully use quotes because they also have a practice of trimming conversation threads from time to time. By not quoting, then the later responses lose context. In the case of this forum, the convention is to never delete things, so that context is preserved.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4