phys to virt and virt to phys help please

phys to virt and virt to phys help please

Post by Stev » Sun, 23 Sep 2001 03:42:03



I am writing KLD driver and want to access physical memory. I assume I will
need to convert to/from virtual memory from looking at some Linux code.

In Linux I see they can use phys_to_virt() and virt_to_phys() to map
physical memory to/from virtual memory.

I have dug into source and found some BIOS conversions, but not generic
conversions.

How do I do these conversions in FreeBSD?

thanks

--

-- Steve --

 
 
 

phys to virt and virt to phys help please

Post by JD » Sun, 23 Sep 2001 05:00:23



Quote:> I am writing KLD driver and want to access physical memory. I assume I will
> need to convert to/from virtual memory from looking at some Linux code.

> In Linux I see they can use phys_to_virt() and virt_to_phys() to map
> physical memory to/from virtual memory.

> I have dug into source and found some BIOS conversions, but not generic
> conversions.

> How do I do these conversions in FreeBSD?

Even though I wrote alot of the code, I haven't looked at it for a few years.

I suggest looking through the pmap module, and hunt for symbols
like pmap_extract or vtophys.  It might have changed.

IN a general case, phys_to_virt makes less sense, because a signal page
can be mapped to multiple virtual addresses, but the specific case of an
individual processes mapping or kernel mapping is possible, and there has
been such a call in the code.

Actually, the pmap code isn't that complicated, and much of it in the area
that you are talking about isn't going to change alot...  As in Linux, the source
code is your friend :-).

I am giving you this answer now, just in case someone else dosesn't give you
more accurate or complete info!!!   If someone else gives you better info,
then listen to them, not me!!!  :-).

John

 
 
 

phys to virt and virt to phys help please

Post by Steve Gotthard » Mon, 24 Sep 2001 16:18:03


Thanks for the reply - I will dig further. Here is what I am doing in case
you know of a better way than virt to/from/phys:
I need to get an address FOR bios FROM bios: I need to hunt bios physical
mem 0xF0000-0xFFFF0 for a signature. After validatiing the checksum, I can
extract the physical address from BIOS that is a  physical pointer address
(0xFxxxx) to the BIOS function I need to call (thus I think I need to do a
phys to virt right?). This is not regular BIOS but for thinkpad SMAPI. It
works similar so I will investigate bios.c more.

thanks again, nothing is easy, but anymore help would be great. If not I
will plod along...

steve





Quote:> > I am writing KLD driver and want to access physical memory. I assume I
will
> > need to convert to/from virtual memory from looking at some Linux code.

> > In Linux I see they can use phys_to_virt() and virt_to_phys() to map
> > physical memory to/from virtual memory.

> > I have dug into source and found some BIOS conversions, but not generic
> > conversions.

> > How do I do these conversions in FreeBSD?

> Even though I wrote alot of the code, I haven't looked at it for a few
years.

> I suggest looking through the pmap module, and hunt for symbols
> like pmap_extract or vtophys.  It might have changed.

> IN a general case, phys_to_virt makes less sense, because a signal page
> can be mapped to multiple virtual addresses, but the specific case of an
> individual processes mapping or kernel mapping is possible, and there has
> been such a call in the code.

> Actually, the pmap code isn't that complicated, and much of it in the area
> that you are talking about isn't going to change alot...  As in Linux, the
source
> code is your friend :-).

> I am giving you this answer now, just in case someone else dosesn't give
you
> more accurate or complete info!!!   If someone else gives you better info,
> then listen to them, not me!!!  :-).

> John

 
 
 

phys to virt and virt to phys help please

Post by mark tingue » Tue, 25 Sep 2001 06:22:58




>Thanks for the reply - I will dig further. Here is what I am doing in case
>you know of a better way than virt to/from/phys:
>I need to get an address FOR bios FROM bios: I need to hunt bios physical
>mem 0xF0000-0xFFFF0 for a signature. After validatiing the checksum, I can
>extract the physical address from BIOS that is a  physical pointer address
>(0xFxxxx) to the BIOS function I need to call (thus I think I need to do a
>phys to virt right?). This is not regular BIOS but for thinkpad SMAPI. It
>works similar so I will investigate bios.c more.

>thanks again, nothing is easy, but anymore help would be great. If not I
>will plod along...

physical addresses are not automatically mapped into kernel virtual space.
on startup, the kernel maps space for itself and some data structures,
but individual device drivers should map the appropriate BIOS/DMA/bus
register areas.

was the physical memory range mapped into the virtual address space by a
driver? It sounds like you are doing the begining of a device driver
and you have not mapped the physical memory into kernel virtual space
yet. when you do, this is the best time to remember the physical
and virtual addresses.

For the next step of using the physical address found in the first step,
the same question is asked has this physical address within the range of
the above mapping, if not then that has to be mapped to a virtual address.

Once the Virtual <> Physical memory has been mapped and you know the
PTE/PDE and pmap, you can go back and forth between physical and virtual
memory addresses, but it is much easier to remember in a variable.

 
 
 

phys to virt and virt to phys help please

Post by JD » Tue, 25 Sep 2001 08:21:12





> >Thanks for the reply - I will dig further. Here is what I am doing in case
> >you know of a better way than virt to/from/phys:
> >I need to get an address FOR bios FROM bios: I need to hunt bios physical
> >mem 0xF0000-0xFFFF0 for a signature. After validatiing the checksum, I can
> >extract the physical address from BIOS that is a  physical pointer address
> >(0xFxxxx) to the BIOS function I need to call (thus I think I need to do a
> >phys to virt right?). This is not regular BIOS but for thinkpad SMAPI. It
> >works similar so I will investigate bios.c more.

> >thanks again, nothing is easy, but anymore help would be great. If not I
> >will plod along...

> physical addresses are not automatically mapped into kernel virtual space.
> on startup, the kernel maps space for itself and some data structures,
> but individual device drivers should map the appropriate BIOS/DMA/bus
> register areas.

Oh yes -- if it hasn't changed, the approach that I used was to
allocate the virtual address (mark already knows this) by doing
something like kmem_alloc_pageable or somesuch.   Some versions
of the code allowed the ability to deny faults (and normal hardware
mappings should require that), and then doing pmap_kenter (or
pmap_enter as appropriate.)    I seem to remember that non
pageable kernel entries can be made by pmap_kenter.

There are also issues of the new fancy I/O mapping code, that others
know more about that I knew at all (or remember.)

So, when normally allocating memory, the seperation between virtual
and physical allocation is somewhat hidden.   When allocating physical
space, then allocating the viritual memory is done, then the mappings
explicitly forced.

It is (or used to be) okay to do a hard kernel mapping, since kernel
mappings mostly don't change from process to process (at least,
normal kernel mappings.)   pmap_enter is more useful when pages
should be managed for paging and things like that.   pmap_kenter
is kind of a hard-mapping.

If I get around to it, I'll look at the code to see what is going on, but
again, there are people who are MUCH MORE familiar with current
practices!!!

John

 
 
 

phys to virt and virt to phys help please

Post by Steve Gotthard » Tue, 25 Sep 2001 09:07:20


Thanks, thats good help.
steve



> >Thanks for the reply - I will dig further. Here is what I am doing in
case
> >you know of a better way than virt to/from/phys:
> >I need to get an address FOR bios FROM bios: I need to hunt bios physical
> >mem 0xF0000-0xFFFF0 for a signature. After validatiing the checksum, I
can
> >extract the physical address from BIOS that is a  physical pointer
address
> >(0xFxxxx) to the BIOS function I need to call (thus I think I need to do
a
> >phys to virt right?). This is not regular BIOS but for thinkpad SMAPI. It
> >works similar so I will investigate bios.c more.

> >thanks again, nothing is easy, but anymore help would be great. If not I
> >will plod along...

> physical addresses are not automatically mapped into kernel virtual space.
> on startup, the kernel maps space for itself and some data structures,
> but individual device drivers should map the appropriate BIOS/DMA/bus
> register areas.

> was the physical memory range mapped into the virtual address space by a
> driver? It sounds like you are doing the begining of a device driver
> and you have not mapped the physical memory into kernel virtual space
> yet. when you do, this is the best time to remember the physical
> and virtual addresses.

> For the next step of using the physical address found in the first step,
> the same question is asked has this physical address within the range of
> the above mapping, if not then that has to be mapped to a virtual address.

> Once the Virtual <> Physical memory has been mapped and you know the
> PTE/PDE and pmap, you can go back and forth between physical and virtual
> memory addresses, but it is much easier to remember in a variable.

 
 
 

phys to virt and virt to phys help please

Post by Stev » Fri, 28 Sep 2001 05:02:37


OK, I get what has to be done. I have dug into code using
http://current.jp.freebsd.org/tour/releng4/frame.html and cscope only to get
nothing done.

Quote:> physical addresses are not automatically mapped into kernel virtual space.
> on startup, the kernel maps space for itself and some data structures,
> but individual device drivers should map the appropriate BIOS/DMA/bus
> register areas.

> was the physical memory range mapped into the virtual address space by a
> driver? It sounds like you are doing the begining of a device driver
> and you have not mapped the physical memory into kernel virtual space
> yet. when you do, this is the best time to remember the physical
> and virtual addresses.

yes this is a driver. I have not mapped memory - I do not know how. My
biggest question seems to be how do I map it?

thanks
Steve

 
 
 

phys to virt and virt to phys help please

Post by Stev » Fri, 28 Sep 2001 06:44:13


I hate to give up and I like the challenge.. so .. here is what I have
cobbled together.. any comments?

...frag...

vm_offset_t SMAPI_virtual;

/* get 128K allocated by the kernel */
SMAPI_virtual = kmem_alloc_nofault(kernel_map, 0x20000);

/* now map the physical onto this allocation */
pmap_map(SMAPI_virtual, 0xE0000, 0xFFFFF, 0);

/* now SMAPI_virtual is my virtual offset in VM which is mapped <> with phys
*/

--

-- Steve --




> >Thanks for the reply - I will dig further. Here is what I am doing in
case
> >you know of a better way than virt to/from/phys:
> >I need to get an address FOR bios FROM bios: I need to hunt bios physical
> >mem 0xF0000-0xFFFF0 for a signature. After validatiing the checksum, I
can
> >extract the physical address from BIOS that is a  physical pointer
address
> >(0xFxxxx) to the BIOS function I need to call (thus I think I need to do
a
> >phys to virt right?). This is not regular BIOS but for thinkpad SMAPI. It
> >works similar so I will investigate bios.c more.

> >thanks again, nothing is easy, but anymore help would be great. If not I
> >will plod along...

> physical addresses are not automatically mapped into kernel virtual space.
> on startup, the kernel maps space for itself and some data structures,
> but individual device drivers should map the appropriate BIOS/DMA/bus
> register areas.

> was the physical memory range mapped into the virtual address space by a
> driver? It sounds like you are doing the begining of a device driver
> and you have not mapped the physical memory into kernel virtual space
> yet. when you do, this is the best time to remember the physical
> and virtual addresses.

> For the next step of using the physical address found in the first step,
> the same question is asked has this physical address within the range of
> the above mapping, if not then that has to be mapped to a virtual address.

> Once the Virtual <> Physical memory has been mapped and you know the
> PTE/PDE and pmap, you can go back and forth between physical and virtual
> memory addresses, but it is much easier to remember in a variable.

 
 
 

phys to virt and virt to phys help please

Post by mark tingue » Fri, 28 Sep 2001 23:21:44




Quote:>I hate to give up and I like the challenge.. so .. here is what I have
>cobbled together.. any comments?

>...frag...

>vm_offset_t SMAPI_virtual;

>/* get 128K allocated by the kernel */
>SMAPI_virtual = kmem_alloc_nofault(kernel_map, 0x20000);

>/* now map the physical onto this allocation */
>pmap_map(SMAPI_virtual, 0xE0000, 0xFFFFF, 0);

>/* now SMAPI_virtual is my virtual offset in VM which is mapped <> with phys
>*/

good job, another way to say about the same thing is :

        va = pmap_mapdev(0xe0000, 0x2000);
                ...
        pmap_unmapdev(va, 0x2000);

--mark tinguely.

 
 
 

1. phys or virt address ??

Hi,

In linux/Documentation/IO-mapping.txt:

.........
This memory is called "PCI memory" or "shared memory" or "IO memory" or
whatever, and there is only one way to access it: the readb/writeb and
.........

 - remapping and writing:
        /*
         * remap framebuffer PCI memory area at 0xFC000000,
         * size 1MB, so that we can access it: We can directly
         * access only the 640k-1MB area, so anything else
         * has to be remapped.
         */
        char * baseptr = ioremap(0xFC000000, 1024*1024);

        /* write a 'A' to the offset 10 of the area */
        writeb('A',baseptr+10);

        /* unmap when we unload the driver */
        iounmap(baseptr);
.........

Here is my question:  what kind of address (phys, virtual or bus) we
        should pass to writeb() ???

If we check writeb() which is a macro defined in <asm/io.h>, we see
a __io_virt() in the macro.  That let me believe the argument of writeb()
should be phys address or io address.  However, in the above example
given by Linus, the writeb() take a virtual address returned by ioremap()
as argument !!!  Is that correct ???  Well, I knew in x86, both

        writeb('A',baseptr+10);
and
        writeb('A',virt_to_phys(baseptr+10));

will give us the same result, but which one is correct ??

                                        Kuang-chun Cheng

2. Need STL and "string" class for Solaris 5.6, C++ 4.2

3. virt. to phys. (vtop) ???

4. Hi

5. SOLN: Please Help Mach 32 Virt Size Mess

6. Better docs available for XDOS?

7. Please Help: Startup problem, can't find where virt ips are being set?

8. term 2.3.3: trsh fail

9. HELP: Mouse dies sometimes when switch virt scrn to/from X

10. Need Apache Help for Virt-CGI-User

11. syscall phys(2) ??

12. SLS103: weird phys/logical parms? ESDI bigdrive

13. Big IDE- fdisk 'different phys/log...'