xmllmx
xmllmx

Reputation: 42245

Is it possible to access physical address 0?

In C/C++, it is not allowed to access data at address 0.

However, the physical memory are numbered from 0. And, in DOS era, the interrupt vector table was located at physical address 0. The first interrupt vector was the handler of the division-by-zero exception.

My question is:

Under what cases is it allowed to access physical address 0?

Upvotes: 6

Views: 3727

Answers (6)

supercat
supercat

Reputation: 81143

The C Standard does not require that platforms provide access to any particular physical memory location, zero or otherwise. While it would be common on many embedded platforms for (char*)0x1234 to access physical location 0x1234, nothing in the C Standard requires that.

The C Standard also does not require that platforms do anything in particular if an attempt is made to access a null pointer. Consequently, if a compiler writer wanted to treat a null pointer dereference as an access to physical location zero, such behavior would be conforming.

Compilers where an access to address zero would be meaningful and useful will typically interpret a null pointer access as an access to location zero. The only problem would be with platforms where accesses to location zero are useful but compiler writers don't know that. That situation can generally only be handled by checking the compiler's documentation for ways to force the compiler to treat a null pointer like any other address.

Upvotes: 1

user3344003
user3344003

Reputation: 21617

Your question is a bit confusing. You ask whether it is possible to "access physical address zero." In a modern paged operating system, applications cannot specify physical addresses at all. Physical addresses can only be accessed through kernel mode.

In virtual memory systems, it became common not to map the first page of virtual memory into the process address space. Thus, accessing virtual address zero would trigger an access violation.

In such systems, it is usually possible for the application to map the first page to the process access space through system services. Even if the page is not there by default, it can be added by the application.

The answer is that it is possible to access physical memory address zero in kernel mode and it is possible to access virtual address zero IF the application maps that page to memory (and the OS allows that).

Upvotes: 0

Non-maskable Interrupt
Non-maskable Interrupt

Reputation: 3911

To access physical address zero, it depends on which platform you are talking. The language has no idea on the underlying addressing model, it depends on the OS.

  • On bare metal environment, you have total control on the page table if paging is enabled, or just de-reference zero if paging is not enabled.
  • On some Unix and Linux variation, you do mmap and perhaps also open /dev/mem to get a non-null pointer with logical address non-zero but physical address zero, it may require some access rights.
  • I'm not sure on Windows.

PS. Other answers seems make a confusion on language level pointer and physical address.

Upvotes: 9

Generally, the address space in virtual memory is managed by the operating system.

A freestanding C (or C++) implementation could certainly allow you to dereference (void*)0 in an implementation specific way. But beware of undefined behavior.

The C and C++ standards are very careful about the NULL pointer (and C++11 added the nullptr keyword for several good reasons).

A C compiler (at least a hosted implementation) is allowed to suppose that after a successful dereference a pointer is not null. Some optimizations in GCC are doing that.

Most hosted C or C++ implementations have a null pointer which is an all-zero-bits word, but that is not required by the standard (however, it is very common; it helps the compiler and the libc). However, pragmatically, a lot of software is supposing that NULL is represented by all-zero-bits (in theory it is a mistake).

I know no C implementation where NULL is not all-zero-bits, but that is not required. However, coding such a compiler would be a headache.

On some operating systems, an application can change its address space, e.g. with mmap(2) on POSIX or Linux.

If you really wanted, you could access address 0 in C, but you really should never want to do that.

Upvotes: 4

ravi
ravi

Reputation: 10733

There's no specification in either C or C++ that would allow you to assign a specific physical address to a pointer. So your question about "how one would access address 0" could be translated to "how one would assign 0 address to a pointer" formally has no answer. You simply can't assign a specific address to a pointer in C/C++.

But you can get that effect through integer-to-pointer conversion:-

uintptr_t null_address = 0;
void *ptr = (void *) null_address;

Upvotes: 3

Mike Seymour
Mike Seymour

Reputation: 254431

In C/C++, it is not allowed to access address 0.

Yes you can, as long as there's addressable memory there. On most platforms, there won't be.

Under what cases is it allowed to access physical address 0?

You can access any physical address if it's mapped into virtual memory. If there's anything sensitive there, then the OS probably won't allow that in user code. Within the kernel, it's just a case of setting up the page tables to include that address.

Upvotes: 4

Related Questions