Strange bugs in paging and bitsets in kernel

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
kfreezen
Member
Member
Posts: 46
Joined: Tue Jul 21, 2009 11:36 am

Strange bugs in paging and bitsets in kernel

Post by kfreezen »

My kernel has a very strange bug. Somehow, my kernel is triple faulting in the bitset implementation's code.

Nine times out of ten it will triple fault. The bit that is my kernel is attempting to set when it triple faults seems entirely random.
However, the range seems fairly consistent. 0x1FFFFA0-0x1FFFFFF

Another weird issue that probably has a very easy fix is happening (that i think mite be related to this issue). For example, the bit storage will be at 0x106000.
Here's the particular function that's causing the error. (I think)

Code: Select all

int SetBits(Bitset* b, int bit_start, int bit_end) {
	kprintf("bit_start=%x, bit_end=%x\n", bit_start, bit_end);
	wait(1);
	register int i;
	if(bit_start == 0) {
		b->bits_before_bit_change = bit_end;
		b->bits_type = on;
	} else if(bit_start == b->bits_before_bit_change && b->bits_type==on) {
		b->bits_before_bit_change = bit_end;
		b->bits_type = on;
	}
	
	/*if((bit_start%32)==0 && (bit_end%32)==0) {
		int iend = bit_end/32;
		for(i = bit_start/32; i<iend; i++) {
			if((i%0x5000)==0) {
				kprintf("loop!%x\n", i*32);
			}
			if(i >= b->length) {
				kprintf("YOU FAILED!!\n");
				return 1;
			}
			b->bit_storage[i] = 0xFFFFFFFF;
		}
		return 0;
	}*/
	
	for(i = bit_start; i<bit_end; i++) {
		if((i%32)==0 && bit_end-i>=32) { /*
			if(i/32 >= b->length) {
				return 1;
			} else {
				// Mark it. duh.
				b->bit_storage[i/32] = 0xFFFFFFFF;
				i+=31;
			}*/
		} else if(SetBit(b, i) == 1) {
			return 1;
		}
		if((UInt32) i>=0x1ffffa0) {
			PutHex(i);
			PrintChar('\r');
		}
	}
	
	return 0;
}
the kprintf()'s, wait()'s and Put*()'s are merely there for debugging.

also, paging code (which is calling the SetBits() function)

Code: Select all

Bitset* bits;
...
#define ONE_TO_ONE_MAP_MB 32
...
void Paging_Init(int kb_of_mem) {
...
        for(i=0; i<(ONE_TO_ONE_MAP_MB/4); i++) {
		kprintf("start=");
		if(SetBits(bits, (i*MEGABYTE*4), ((i+1)*MEGABYTE*4))==1) {
			kprintf("Error");
		}
		kprintf("middle=");
		PageTable* pt = IdMapPageTable(i);
		
		pd->d[i] = AssemblePDE((Pointer) pt->t, 0x7);
		kprintf("%x %x, %x\n", i, (i*MEGABYTE*4), ((i+1)*MEGABYTE*4));
	}
...
}
Anyway, any help would be greatly appreciated.
Last edited by kfreezen on Mon Oct 31, 2011 12:03 am, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Strange bugs in paging and bitsets in kernel

Post by Combuster »

Always post bochs logs on a crash.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
kfreezen
Member
Member
Posts: 46
Joined: Tue Jul 21, 2009 11:36 am

Re: Strange bugs in paging and bitsets in kernel

Post by kfreezen »

I'm sorry. It is qemu and AFAIK it does not have that capability. Anyway, I believe I resolved the issue. The problem was that I was not allocating memory of the proper size for the bitset. anyway can anyone explain how 0x106000+0xb00000 = 0x3000000? (It will probably be another simple fix so don't bother answering unless you really want to.)

EDIT: Found a workaround for the above issue of 0x106000+0xb00000 = 0x3000000. It may have been due to adding a UInt32 pointer to a UInt32.

May others not show the same ignorance I have perceived myself to have shown :wink:
Post Reply