[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How should learning to program in c++ be approached, if learning objectives are sought to be customised?



On 2022-05-30 13:01, Susmita/Rajib wrote:
I was trying to find out if c++ could be used to build device drivers.
You said, "... Linux drivers are written in C, but technically you can
mix languages: use C++ and link it against C ...". But I would request
more specificity here:    (a)  if c++ could be used without using any
other programming language to build a device driver. (b)  If it is
practised industrially.

I work on a kernel mode driver for windows. It is written in C++. But we can't use the C++ standard library, as Windows kernel mode doesn't support exceptions. I think someone managed to make a version that did but it never ended up being good enough to catch on.

Also stack space is tiny in kernel mode so you can't put much on the stack.

And you can't allocate memory at high IRQ level (when handling interrupts or in certain calls you will get from the OS).

Also at the high IRQ levels you can't access paged memory. Because you will die if the memory is not paged in (you can't have a page fault).

This means you have to deal with paged and non paged memory specifically with intention.

Also all kernel mode OS functions in windows (Linux syscall equivalents in windows) are C functions (as are most user mode OS functions in windows).

So you are limited to languages that allow you to control allocation (paged vs non paged) and deal with a stack size of a few kilobytes. And also to deal with CPU physical vs virtual memory vs GPU physical and virtual memory (I work on a graphics driver). So that means pointers.

So everything except assembly, C and C++ is out. Maybe rust I don't know but I think rust has something to manage allocations and someone would have to rewrite that to work in kernel mode and deal with all that.

So again your choices in practice are assembly, C, a subset of C++ (essentially C + classes, but no standard library and practically no templates due to stack limitation), maybe rust or some language you design yourself.

Best wishes,
Bijan


Reply to: