Linux kernel memset - what is the address - memory

What is the address which memset works in kernel space? I suppose that it is kernel virtual address. So if I want to reprint some memory represented by vm_area_struct userspace virtual memory address I need to obtain corresponding struct page * areas and then do kmap() to get kernel virtual address.
Am i right?

Related

What is the size of memory in this diagram?

I want to ask some questions about this diagram that showing the main memory with OS and different processes : how can I compute the size of main memory in Kbytes ? and What will happen if Process B generates a logical address of 200? Will the CPU return a
physical address or error?
I'd assume the unlabeled numbers on the left are addresses in bytes; which would imply there's 2048 bytes (or 2 KiB) of something (virtual space, or physical space, or maybe even RAM if there's no devices mapped into the physical space). Of course it could just as easily be 2048 bits, or 2048 (36-bit) words, or..
If Process B tries to access logical address of 200; it might work (no security), or it might cause some kind of trap/exception because the process doesn't have permission to access the operating system's area; or it could be impossible for the process to do that (e.g. maybe the design of the CPU restricts the process to unsigned offsets from a base address of 1203).

How do you register a region of user space memory in a kernel module?

I am working on a Linux module to interface with a third-party device. When this device is ready to give my module information, it writes directly to the RAM memory address 0x900000.
When I check /proc/iomem, I get:
00000000-3fffffff: System Ram
00008000-00700fff: Kernel code
00742000-007a27b3: Kernel datat
From, my understanding, this means that it is writing to an address that is floating out in the middle of user-space.
I know that this is not an optimal situation and it would be better to be able to use memory-mapped addresses/registers, but I don’t have the option of changing the way it works right now.
How do I have my kernel module safely claim the user space memory space from 0x900000 to 0x901000?
I tried mmap and ioremap but those are really for memory-mapped registers, not accessing memory that already ‘exists’ in userspace. I believe that I can read/write from the address by just using the pointer, but that doesn’t prevent corruption if that region is allocated to another process.
You can tell the kernel to restrict the address for kernel space by setting the mem parameter in the bootargs :
mem=1M#0x900000 --> instructs to use 1M starting from 0x900000
you can have multiple mem in boot args
example: mem=1M#0x900000 mem=1M#0xA00000
Following command should tell you the memory region allocated to the kernel:
cat /proc/iomem | grep System

pagination - virtual addresses, physical addresses, mapping - considerations

Pagination in some processor make it possible to map virtual address
(A2345678) to physical address (823C5678). However, it is not possible
to map virtual address (345678) to (2ABC678). What can we conclude
about size of frame, page, size of virtual memory, size of physical
memory.
What I think about it:
(A2345678) -> (823C5678)
So, size offset is most 19 bits. We know that size of page (and frame) has size at most 219, like in my previous question.
When it comes to size of virtual memory, and physical memory - I can conclude nothing.
Similary, I don't know what tell me information about non-possibility mapping address.
Can you try to explain it me ?
I do see something we can conclude after all:
If a mapping to physical address 0x823C5678 is possible, physical memory is at least that large. (Assuming there aren't any holes in physical address space; not a good assumption on real hardware, but whatever. We can tell that physical address space is at least that big, even if it doesn't all map to DRAM or MMIO).
Similarly, the valid virtual address 0xA2345678 gives us a lower bound on the virtual address size. Presumably all the virtual address bits can be 1, so the highest possible virtual address is at least 0xFFFFFFFF. i.e. virtual addresses are at least 32 bits, but could be any larger size.
This reasoning applies to physical address space, but not the size of physical memory. (e.g. in a computer with 19GiB of RAM, the highest valid physical address isn't 2n-1.)
The fact that you can't map 0x345678 to 0x2ABC678 does tell us that the page size is greater than 212. The physical address is below the address that was mappable, so we can rule out that possible reason for the mapping being impossible. I think too high and misaligned are the only possible reasons for a mapping not being possible.
(0xc = 0b1100, while 0x5 is 0b0101, so the common bits are only 0x678.)
We can assume that physical memory is a whole number of pages, so we can round up the lowest possible end of physical memory to the next multiple of 213.

Address line, 16 bit memory, and addresses

What is an address line (memory), and how many addresses does one address line hold. This would be in a 16 bit memory
Usually "Address line" denotes the electrical connection between a single address bit of the CPU (after translation by a memory management unit from a virtual address to a physical address) and the memory. A 16-bit CPU usually has 16 address lines (if the physical memory space is not expanded by a memory management unit).
So, one address line alone could only address 2 addresses, because its state can only be be 0 or 1, and a processor with 16 address lines can address 2^16 addresses, i.e. 64k addresses.

Inquiry on printf

when we print an address of a variable, which address gets printed?
if it is virtual memory, then why is it so?
can any one explain some more...
On modern desktop/server OSs, all memory is virtual memory. I'm not aware of any way to access the underlying physical addresses from outside of the kernel. Even if it is possible, it's not going to be useful in the vast majority of situations.
So, if you do printf("%p", (void*)&variable); it will print the virtual address of variable for the current process.
The virtual memory address gets printed, and it is so because you don't have any need for the physical address, and the whole point of the OS is to prevent you from having to deal with physical addresses (well it's not only that, but it's also that :D).
On a normal PC computer, it is a value which you get if you convert poitner to integer of the same size.
void *p = something;
int i = *(int*)p;
printf("%x", i);
Memory address is virtual, yes of course, because that's how the process executing your code addresses the memory in your comupter. The process cannot see physical memory.

Resources