Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was reading "Linux device drivers, 3rd edition" and faced a few kernel items I don't quite understand. Hope gurus on this forum will help me out.

  1. Does the Linux kernel internally operate with virtual or physical addresses? What especially confuses me is that there are several types of addresses (logical, virtual, bus and physical) and they are all valid and operable by the kernel.

  2. Is that correct that CPU instructions can't directly address data stored in peripheral devices and therefore addressable memory is used, i.e. buffers, for these purposes?

  3. Can a process sleep when requesting a semaphore (which has a value 0) and has to wait for it?

  4. Atomic operations -- are these guaranteed by specific CPU instructions?

share|improve this question

2 Answers 2

I can try to answer 1,3 and 4.

The Linux kernel uses different steps to transition program code into electrical signals.

Logical Address: Those are included in the machine language instructions to address an operation or instruction. Divided into segment and offset.

Linear Address: The Segmentation Unit translates logical into linear addresses. This is a hexadecimal number (on 32bit architecture: 0x00000000-0xffffffff) addressing the space in memory.

Physical Address: Further, the Paging Unit converts linear into physical addresses. Those are electrical signals addressing memory cells over pins on microprocessors.

Bus Address: Used by all hardware devices except the CPU to address memory cells (DMA does not require the CPU, but still addressing). These addresses are mostly identical to the physical ones, except on some other architectures, like SPARC and Alpha which include a separate I/O Memory Management Unit.

The kernel operates with all addresses, and every of them is a step between the request of a user and the actual processing of this request on hardware level.


If a process approaches a semaphore with value 0 or lower, he gets suspended until the value reaches 1 or more. This only occurs to processes which can sleep. An interrupt handler cannot sleep and therefore is forbidden to use semaphores.


Atomic operations can be achieved by using Assembly language instructions, those are defined by:

  • Zero or one memory access
  • Prefixed with LOCK_PREFIX

On C level, the kernel provides the type atomic_t and macros prefixed with atomic_
(which add LOCK_PREFIX to the assembly instructions).

share|improve this answer

It's better not to ask multiple questions at once as not everyone can or will answer everything. Still, I'll give a short answer to each.

Does the Linux kernel internally operate with virtual or physical addresses? What especially confuses me is that there are several types of addresses (logical, virtual, bus and physical) and they are all valid and operable by the kernel.

Yes. Different parts of the kernel use different address spaces.

While kernel code is processing a system call, its memory mappings include both the whole kernel memory space and the whole memory space of the process (unless your kernel has high memory configured in, but that's too complicated to go into here). These are all logical (a.k.a. virtual) addresses: the high-order bits of the address indicate which page to look up in the MMU, and the low-order bits are a linear address inside the page. The memory mappings inside the MMU change whenever a task switch occurs (changing the page table in the MMU is a big part of a task switch).

Some device drivers need to manipulate memory addresses that are valid for the device they are driving. These are often physical addresses, although some architectures have an IOMMU so that devices also see logical addresses of their own. Of course, the memory management subsystem in the kernel needs to manipulate lots of different kinds of addresses.

Is that correct that CPU instructions can't directly address data stored in peripheral devices and therefore addressable memory is used, i.e. buffers, for these purposes?

This is architecture-dependent. Most architectures do have some kind of DMA (direct memory access), that allows at least some communication with devices to be done via RAM. Additionally, on some architectures (e.g. ARM) all device accesses are done with load and store instructions at appropriate addresses, while others (e.g. i386) have specific processor instructions for that purpose. See Memory-mapped I/O for more details.

Can a process sleep when requesting a semaphore (which has a value 0) and has to wait for it?

Yes, taking a semaphore (down and friends) is a blocking operation. This is well-explained in the book.

Atomic operations -- are these guaranteed by specific CPU instructions?

Yes. The details are very architecture-specific. All platforms intended for multitasking provide at least one atomic primitive for synchronization such as compare-and-swap, test-and-set, load-link+store-conditional, etc. In addition to using the correct primitive, the code may need to take care to use proper memory barriers on multiprocessor systems. The Linux kernel provides an implementation of its synchronization primitives for each architecture it supports, so that you in turn only need to use the kernel's portable primitives.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.