X86 asm experts: moving large blocks of data.

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.
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: X86 asm experts: moving large blocks of data.

Post by nullplan »

bzt wrote:Only if you're a noob programmer. I know for sure that my kernel is working okay, because it is I who wrote it, and I made lots of tests and run-time checks. Are you suggesting that you don't trust your own code?
No, I don't. For the same reason you trust yours, I distrust mine: I wrote it. I trust this thing to run inside of an emulator, and maybe on real hardware that I own, but I would not wish that thing on anyone who is not myself. Testing is important and does reveal flaws. However, due to the problem of combinatorial explosions, it is practically impossible to test all combinations of decisions and conditions that might occur in any program of any real complexity. Therefore I rely on ongoing runtime detection of problems, vulgo CPU exceptions. And otherwise on reading code to find bugs. But of course, I keep missing stuff, given that I am cursed with that imperfect pound of grey matter between my ears, that gets constantly distracted.
bzt wrote:And how do you plan to recover from a kernel panic? Let me guess, by rebooting the entire computer?
That is the custom, yes. Of course, only after collecting all debug info I could think of, in order to aid me in discovering the cause of the flaw and to correct it. I'm well aware that I'm not exactly writing Minix.
bzt wrote:...which will detected as the TCB starts with a magic. It doesn't happen, but even in the unlikely event if it does, my kernel is capable to handle that gracefully, kill the clobbered process (and only that one).
Unless it was null pointer access with offset (e.g. array or structure access through a pointer, and the pointer was null). And even so, I know many applications where that is really not better than rebooting. Embedded applications often have a small number of processes, but those are really important, and if any of those is killed, the machine ceases to function. A reboot would also cause a break in functionality, but only for a limited time. And in many cases, simply restarting the killed application without a reboot would also not work.
bzt wrote:That way you must use SWAPGS, which is a) known to be vulnerable to attacks, b) using segmentation makes your kernel unportable.
Well, point a) really means that x86 is just fundamentally insecure. However, I think I can patch that: If I detect a GPF in userspace while IP is pointing to a SWAPGS instruction, I can just flush all caches. So no speculative cache timing attack there, because when the "privileged opcode" handler runs, absolutely nothing will be in cache. Is what I imagine, anyway. The use of SWAPGS is all but mandatory if kernel address space is fixed, multiple CPUs are supported, and SYSCALL is the system call method of choice. In that case the syscall handler will be entered with SP pointing to the user stack, and saving things in global variables is going to be a problem due to the multiple CPUs. Of course, if you put the TCB (or the CPU-local structure, now that I think about it) at a fixed virtual address and keep swapping that out, that problem is fixed. And for that it doesn't even matter what that fixed address is (so 0 works as well as any other free address). It's just harder conceptually (because now I can't share any pointers into that memory area with any other context, which is a restriction not placed on any other kernel address).

As for point b), well it would if I wasn't using %gs (or rather, GS.Base) merely as a register holding a pointer. Every other architecture has a thread pointer register I can use (PowerPC has R2, Microblaze has R21, ...), the x86 version is merely unbelievably awkward to use.
bzt wrote:I would recommend to write your kernel properly instead, so that it can't have NULL pointer dereferences in the first place. You can't guarantee that all applications are written correctly, but you can guarantee at least that the kernel that you wrote is free of NULL dereferences. It shouldn't be a problem for an experienced programmer to write correct code. That's why they are called "experienced".
I am experienced, that is why I know a bit about what I can and cannot do. I can slap together a kernel that won't crash if you look at it wrong. I cannot write bug-free software. That grey matter getting in the way again.

Also, your claim of being able to write correct code would be a lot more convincing if this thread did not exist. Everyone makes mistakes, and I don't trust myself to find all of them before they blow up in my face.
nexos wrote:What does TCBs have to do with moving blocks of data?
Nothing. Sometimes conversations evolve and go off-topic. In this case from memory copy via Linus Torvalds' design philosophy to this.
Carpe diem!
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: X86 asm experts: moving large blocks of data.

Post by bzt »

nexos wrote:Bugs affect every programmer just the same.
Absolutely not. Experienced programmers make far less bugs in the first place, and they need far less time to locate and fix them.
nullplan wrote:For the same reason you trust yours, I distrust mine: I wrote it.
Okay, there's nothing to add to this.
nullplan wrote:I cannot write bug-free software. That grey matter getting in the way again.
This means you're not experienced, and there's a long road ahead of you. Please don't take this an attack, but a friendly advice: learn about automated testing, unit testing etc. specially if you're not confident in your programming skills. After more than 30 years of programming I've reached a level where I can write a very complex code in the first iteration in which valgrind can't find anything. If you're presistent enough, you can reach this level too.
(I'm not saying that all code that passes valgrind is necessarily good, but it is a very important milestone for sure.)

Cheers,
bzt
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: X86 asm experts: moving large blocks of data.

Post by kzinti »

LOL.
StudlyCaps
Member
Member
Posts: 232
Joined: Mon Jul 25, 2016 6:54 pm
Location: Adelaide, Australia

Re: X86 asm experts: moving large blocks of data.

Post by StudlyCaps »

:roll: Nobody is perfect, making bug tolerant software is a prudent and intelligent decision no matter how experienced you are.
Post Reply