Page 1 of 1
spinlock problem
Posted: Wed Jul 16, 2008 10:22 am
by itisiuk
this code was working but now its generating a "matching constraint does not allow register" error, i dont know whats wrong or why,
Code: Select all
typedef unsigned char SpinLock;
INLINE void lockSpinlock(SpinLock *sp)
{
register SpinLock tmp = 1;
do
{
__asm__ __volatile__("xchgb %0,%1"
: "=r"(tmp), "=m"(*sp)
: "0"(tmp), "1"(*sp));
} while ( tmp );
}
INLINE void unlockSpinlock(SpinLock *sp)
{
register SpinLock tmp = 0;
__asm__ __volatile__("xchgb %0,%1"
: "=r"(tmp), "=m"(*sp)
: "0"(tmp), "1"(*sp));
}
where INLINE is defined
Code: Select all
#define INLINE static __inline__ __attribute__((__always_inline__))
ive been implementing the spinlocks by
Code: Select all
static SpinLock freePageMutex = 0;
lockSpinlock(&freePageMutex);
unlockSpinlock(&freePageMutex);
i havent changed anything since it was working, all i have done is added more implmentations of this method. but even after i removed the one that stoped it working and even after reverting to my backup it wont work at all now.
can anyone tell me whats wrong here?
if anyone can fix it for me i would be very greatfull cos im really having bad luck with my kernel at the min and i dont think i can actually fix this.
Re: spinlock problem
Posted: Wed Jul 16, 2008 10:46 am
by jnc100
I'm not entirely sure of your problem, especially as it magically stopped working suddenly. Did you change your version of gcc? The xchg (b) instruction requires one operand to be a register and the other to be a memory operand. Maybe the problem is that your input arguments should also be of type "r" and "m". Anyhow, your spinlock implementation is not perfect. The algorithm I use for a lock is:
1) keep looping until spinlock not set (i.e. =0, you shouldn't use a lock instruction here as it locks up the whole bus intermittently whilst waiting)
2) attempt to set, test for not set at same time (e.g. lock cmpxchg), the rationale here is that another process could capture the lock between 1 and 2.
3) if set fails (e.g. already set), loop back to 1.
You can optimise the wait loop with the pause instruction on P4 and above, it is ignored on others.
Unlock is simply test whether the spinlock is set by this thread and unset if it is.
My (previous kernel's) implementation is
here with versions that either do or do not disable interrupts for the duration of the lock being set.
Regards,
John.
Re: spinlock problem
Posted: Wed Jul 16, 2008 12:56 pm
by itisiuk
Thanks
i hadnt change the version of gcc either.
im starting again now. the whole thing fell apart for no apartent reason at all.
i tryed removing all the locks and replacing them with a version based on yours but got the same error in your code.
even tryed replacing them with the mutex code i have used for years without problem and the same problem occurs in that.
its not the routines using the locks themselfs either, i tryed compiling with the locks removed all together and i get no errors at all.
so god knows what the hell happend there. hopfully ill be able to salvage the virtual mem manager, i had just got that working too and was looking forward to testing it.
Re: spinlock problem
Posted: Wed Jul 16, 2008 4:07 pm
by Adek336
"matching constraint does not allow register" seems to be a warning not an error according to a quick google search; do you use -Werror ?
Re: spinlock problem
Posted: Wed Jul 16, 2008 4:47 pm
by itisiuk
Yeah, i did, i removed it to see if that was the problem, and it compiles havent tested it yet.
its odd cos thats been there all the time and its never produced that error b4 and the code was there at the same time.
but i gotta say thanks, ive been having real bad luck with my kernel that past month. fixing one thing and a hundred other things seem to go wrong.
i hope it works now when i get change to test run it
cos ill have graphics working for the 1st time in protected mode as well as my virt mem manager
which means all this has been worthwhile in the end
otherwise
Re: spinlock problem
Posted: Thu Jul 17, 2008 10:17 am
by Adek336
good luck :d it must be cool to see graphic modes.
as for the constraint problem, -Werror makes gcc treat warnings as errors and obviously this is a new warning introduced not long ago and although you haven't explicitly upgraded gcc, did you use some packaging system like apt-get or yum to install software? The package might have required an upgrade of gcc.
Re: spinlock problem
Posted: Thu Jul 17, 2008 1:34 pm
by itisiuk
no, i hadnt done anything thats why i was really confused.
anyway, it didnt work when i tested it
, it just locked up.
but ive managed to salvage some of it so far and wrote some new locking mechanisms.
just tring to salvage as much as i can and theres alot of code now so its going to take a while to test and rewrite everything
with the new locks and some other things ive decided to add.
so hopfully by the end of the month i should finnaly have graphics working.
currently the new version is working but in text mode
Re: spinlock problem
Posted: Thu Jul 17, 2008 1:39 pm
by Adek336
there's a lot of interesting things to do with TUIs.. do you have a shell?
btw, I wonder how hard it is to have a fairly well developped minimalistic window manager. I think I'll check if the wiki says anything about it.
Message body:
Enter your message here, it may contain no more than 60000 characters.
Is it just me or is 60000 characters rather a amusingly arbitrary amount ?
Re: spinlock problem
Posted: Thu Jul 17, 2008 3:07 pm
by itisiuk
well since ive just started re-writing i dont have a shell now either, just debug messages in text
.
actually having a well developed windows manager is not that hard to write once you have all the graphics functions and mouse functions in place.
i origonally started writing my os in turbo c 3 and i had a fully working gui before i could access a floppy disk had a decent mem managers or anything really.
before that i had wrote one entirely in real mode which was even better and had access to the floppy drive, but it wasnt much use though.
basically just write a function to create a desktop then window, then button and so on just breaking everything into smaller pieces
and make sure that things like the window have a pointer to the desktop on which it is drawn.
if i can find some of my old testing code ill post it up here, dunno when that will be tho cos i start my new job 2morrow. but ill try and dig it
out at the weekend.
the code was origanally for dos as little front end project i did for it, before i started to try and write my own os.
Re: spinlock problem
Posted: Sat Jul 19, 2008 5:51 am
by JamesM
Hi,
While I can't pinpoint exactly where your error is, it can be avoided by using GCC's internal builtin functions:
Will do the xchg atomically and return true if it succeeded or false if it failed.
Will do the xchg atomically and return the value that it found at the memory location.
The advantage to using these builtins is that they work on most platforms - so our spinlock code is identical for all our targets.
You can find more information about gcc's atomic builtins
here - there are all sorts of nice ones like __sync_fetch_and_add(), __sync_fetch_and_sub, etc that can be used for making semaphores too...
Re: spinlock problem
Posted: Sat Jul 19, 2008 7:41 am
by itisiuk
Thanks JamesM
i ended up having to rewrite everything again anyway. and got the spinlocks working now.
dunno what happend with the last set, but ill leave it to one of lifes mystries i think.
also just tested my new more core routines and added dlmalloc. so far it allocates 200Mb in one go without a problem. unfortunateley i cannot test it any higher cos i only got 512Mb ram and vmware is using 316Mb
also its all contiguous allocated.
soz i just had to get that in although its entirey irrelivant, i was just so happy when it worked with the locking mechanism too.
also heres some of my old window manager routines from when i 1st started writing them. i carnt find anymore there on my old hard drive so it may be next week before i can dig them out.
the one ive posted was for dos and written using tcc 3.
Re: spinlock problem
Posted: Sat Jul 19, 2008 8:19 am
by itisiuk
also as a .zip as i cant convert it to .tar.gz soz