Page 1 of 1

Strange code result

Posted: Wed Sep 16, 2009 8:23 am
by FlashBurn
I have the following function:

Code: Select all

static inline uint32t _bsf(uint32t val) {
	uint32t res;
	
	asm volatile("bsf %[output],%[input]"
		:[output] "=q"(res)
		:[input] "q"(val));
	
	return res;
}
So in one of my functions I have this code:

Code: Select all

printf("4kb: %#X, bsf: %X\n",bitmap4kb[slot4kb],_bsf(bitmap4kb[slot4kb]));
printf("addr bitmap4kb[]: %#X\n",&bitmap4kb);
printf("4kb: %#X, bsf: %X\n",bitmap4kb[slot4kb],_bsf(bitmap4kb[slot4kb]));
Ok and this is the output I get:

Code: Select all

4kb: 0x2, bsf: C0106184
addr bitmap4kb[]: 0xC0106160
4kb: 0x0, bsf: 3D5
How can that happen???? I don´t know where I should search for the error.

Re: Strange code result

Posted: Thu Sep 17, 2009 7:55 am
by FlashBurn
Ok, as always was the error some problem with the at&t syntax :(

This is the corrected function for _bsf:

Code: Select all

static inline uint32t _bsf(uint32t val) {
	uint32t res;
	
	asm volatile("bsf %[input],%[output]"
		:[output] "=q"(res)
		:[input] "q"(val));
	
	return res;
}

Re: Strange code result

Posted: Sun Sep 20, 2009 4:21 pm
by FlashBurn
Some new really cool results of my coding session:

Code: Select all

ptrEntry= (struct slabEntry_t *)(kernelArgs->kernelMemEnd + 0x1000 - sizeof(struct slabEntry_t));
ptrEntry->prev= 0;
ptrEntry->next= 0;
printf("partial: %#08X, free: %#08X, entry: %#08X\n",slabDesc->partial,slabDesc->partial->free,ptrEntry);
ptrEntry->free= kernelArgs->kernelMemEnd;
printf("partial: %#08X, free: %#08X, entry: %#08X\n",slabDesc->partial,slabDesc->partial->free,ptrEntry);
ptrEntry->used= 0;
ptrEntry->base= kernelArgs->kernelMemEnd;
Result:

Code: Select all

partial: 0xC010BFEC, free: 0xC010B080, entry: 0xC010CFEC
partial: 0xC010BFEC, free: 0xC010C000, entry: 0XC010CFEC
So my question is, how can a pointer change something at which it doesn´t point? The thing is, that it could be a problem with gcc, because the value which changed is pointed by the old value in the pointer.

I will have a look at the assembly output of this code, but maybe I also don´t see something there.

Re: Strange code result

Posted: Mon Sep 21, 2009 3:58 am
by FlashBurn
Ok, after a really long debugging session I found an error in my pmm and so the same physical addr got mapped to 2 different virtual addrs. Because of that it was possible that a pointer could change something it didn´t point to.