Skip to contentSkip to conversation

Calling function from address

DISCUSSION Micha M. 5 participants 9 messages Jun 7, 2011 – Jun 11, 2011 1 message not in the archive

lore
Share & tools
lore

Micha M.

author
#0 lore raw

Hi!

Is it possible to call a function that is somewere in the physical-address space? So I'd like to jump to a certain physical address, execute the code there and then return to my kernel module.
I already tried to ioremap that address and cast the new address to a funtion pointer and then call the function, but there where some page faults.

Regards

#micha

Signature · 1 line
/* To err is human; to really fuck things up requires the root password */
#1 lore raw
Quoted 11 lines

-----Original Message-----
From: kernelnewbies-bounces at kernelnewbies.org [mailto:kernelnewbies- bounces at kernelnewbies.org] On Behalf Of Micha M.
Sent: Tuesday, June 07, 2011 1:47 PM
To: kernelnewbies at kernelnewbies.org
Subject: Calling function from address

Hi!

Is it possible to call a function that is somewere in the

physical-address

Quoted 1 line

space? So I'd like to jump to a certain physical address, execute the

code

Quoted 4 lines

there and then return to my kernel module.
I already tried to ioremap that address and cast the new address to a funtion pointer and then call the function, but there where some page faults.

Just guessing, but I suspect that ioremap() does not enable execute permission in the virtual memory pages it allocates to the physical address and that's why you are getting the page fault.

You might have to muck with the PTEs directly to enable execution.

Quoted 7 lines

Regards

#micha

--
/* To err is human; to really fuck things up requires the root

password */

Quoted 5 lines

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

#2 lore raw
In reply to Micha M.
Quoted 9 lines · Micha M.

Hi!

Is it possible to call a function that is somewere in the physical-address space? So I'd like to jump to a certain physical address, execute the code there and then return to my kernel module.
I already tried to ioremap that address and cast the new address to a funtion pointer and then call the function, but there where some page faults.

Interesting, and after page fault....the code is still not executed?

what code(s) do you call? user mode? kernel mode?

Signature · 7 lines
regards,

Mulyadi Santosa
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com
author
#3 lore raw
Quoted 11 lines · Mulyadi Santosa
Quoted 9 lines · Micha M.

Hi!

Is it possible to call a function that is somewere in the physical-address space? So I'd like to jump to a certain physical address, execute the code there and then return to my kernel module.
I already tried to ioremap that address and cast the new address to a funtion pointer and then call the function, but there where some page faults.

Quoted 3 lines

Interesting, and after page fault....the code is still not executed?

what code(s) do you call? user mode? kernel mode?

I don't think that the code was executed. There was a kernel Ooops direct after the page fault and a register dump was printed. The code I what to call is located in ROM and is mapped to the physical address space. Is it possible to configure the kernel to ignore certain address areas and allow calls to that space?

And that code needs to be executed from that place, since it contains pointer in the physical address space. (s oremapping won't work because there are absolute jumps in that function I'd like to call)

The only solution I kan think of at the moment is to solve it somehow in assembly. But I don't know how...

regards,

#micha

Signature · 1 line
/* To err is human; to really fuck things up requires the root password */
#4 lore raw

This is my function :

static spinlock_t xgr_learn_lock = SPIN_LOCK_UNLOCKED;

static int piga_seq_cpt = 1;

/*
* Function called for each systemcall (Hook SELinux avc function) */
int piga_control(u32 ssid, ...., struct av_decision * avd) {

/*
* Here my hypercall work but block my vm with this error :

* " BUG: scheduling while atomic ... "
*/

spin_lock_bh(&xgr_learn_lock);

if ( in_atomic())

           kvm_hypercall2 ( 6, (unsigned long)2 ,(unsigned long)piga_seq_cpt);
  spin_unlock_bh(&xgr_learn_lock);   

if (piga_on == 1) {

/*
* Here my hypercall make a kernel panic with this error:
* " divide error: 0000 [#1] SMP"
*/

                spin_lock_bh(&xgr_learn_lock);

                set_current_state(TASK_UNINTERRUPTIBLE);

                kvm_hypercall2 ( 6, (unsigned long)2 ,(unsigned long)piga_seq_cpt);

                set_current_state(TASK_RUNNING);

                spin_lock_bh(&xgr_learn_lock);

}

}

Quoted 32 lines

Date: Wed, 8 Jun 2011 12:50:57 +0200
From: kernelnewbies at mail.i88.de
To: kernelnewbies at kernelnewbies.org
Subject: Re: Calling function from address
CC: mulyadi.santosa at gmail.com

Quoted 11 lines · Mulyadi Santosa
Quoted 9 lines · Micha M.

Hi!

Is it possible to call a function that is somewere in the physical-address space? So I'd like to jump to a certain physical address, execute the code there and then return to my kernel module.
I already tried to ioremap that address and cast the new address to a funtion pointer and then call the function, but there where some page faults.

Quoted 3 lines

Interesting, and after page fault....the code is still not executed?

what code(s) do you call? user mode? kernel mode?

I don't think that the code was executed. There was a kernel Ooops direct after the page fault and a register dump was printed. The code I what to call is located in ROM and is mapped to the physical address space. Is it possible to configure the kernel to ignore certain address areas and allow calls to that space?

And that code needs to be executed from that place, since it contains pointer in the physical address space. (s oremapping won't work because there are absolute jumps in that function I'd like to call)

Quoted 15 lines

The only solution I kan think of at the moment is to solve it somehow in assembly. But I don't know how...

regards,

#micha

Signature · 6 lines
/* To err is human; to really fuck things up requires the root password */

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110608/3a441ecd/attachment-0001.html

author
#5 lore raw
In reply to Micha M.
Quoted 11 lines · Micha M.

Hi!

Is it possible to call a function that is somewere in the physical-address space? So I'd like to jump to a certain physical address, execute the code there and then return to my kernel module.
I already tried to ioremap that address and cast the new address to a funtion pointer and then call the function, but there where some page faults.

Quoted 3 lines

Regards

#micha

So maybe I have to explain some more. There is some code located in the pysical address space and I need to call it from a kernel module. The problem is, that the code must be run from that location it is stored (it contains absolute jumps). So I'd like to be able to run that code in that address space, or to "tell" the keeernel to ignore page faults/memory protection on a certain address range, so that I can jump there run the code and return to the caller (kernel module)

Regards

#micha

Signature · 1 line
/* To err is human; to really fuck things up requires the root password */

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#7 lore raw
Quoted 9 lines · Gavin Guo
Quoted 7 lines

So maybe I have to explain some more. There is some code located in the pysical address space and I need to call it from a kernel module. The problem is, that the code must be run from that location it is stored (it contains absolute jumps). So I'd like to be able to run that code in that address space, or to "tell" the keeernel to ignore page faults/memory protection on a certain address range, so that I can jump there run the code and return to the caller (kernel module)

What is the architecture do you use? ex: x86, arm, mips,...

ARM.

Quoted 12 lines

I know in some platform like andes, it is possible to turn off the virtual memory.
Then you can jump to the physical address. After doing what you want, turning on virtual memory again. Finally, system return to the normal operation. However, the
code is a little tricky. Before turning off the virtual memory, you must lock the
code jumping to physical address in cache. Otherwise, behaviors, after turning off
the cache, is unpredictable.

Gavin Guo

Signature · 1 line
/* To err is human; to really fuck things up requires the root password */
#8 lore raw

For ARM, MMU info can be found here:

http://infocenter.arm.com/help/topic/com.arm.doc.ddi0333h/Babbhigi.html

That is the theory behind MMU in ARM....but if u want the high level API, look
under the arch/arm/mm directory lots of examples there. Otherwise u
might try the following steps as proposed by "Marco Wang" - google for
it. To quote:

phys_to_virt() only works with directly mapped physical address. I don't think using phys_to_virt() is the best idea, anyway. Usually you do this in several steps in a device driver:

1. Call request_mem_region() to request virtual memory region;
2. Call ioremap() to map physical address to virtual address;
3. Read/write mapped virtual address by using iowriteXX() / ioreadXX(), etc. Here XX can be 8, 16, or 32 for example, represents bit width.
4. Call iounmap() and release_mem_region() to release memory mapping;

Thanks,
Marco Wang

Quoted 26 lines · Micha M.
Quoted 9 lines · Gavin Guo
Quoted 7 lines

So maybe I have to explain some more. There is some code located in the pysical address space and I need to call it from a kernel module. The problem is, that the code must be run from that location it is stored (it contains absolute jumps). So I'd like to be able to run that code in that address space, or to "tell" the keeernel to ignore page faults/memory protection on a certain address range, so that I can jump there run the code and return to the caller (kernel module)

What is the architecture do you use? ex: x86, arm, mips,...

ARM.

Quoted 12 lines

I know in some platform like andes, it is possible to turn off the virtual memory.
Then you can jump to the physical address. After doing what you want, turning on virtual memory again. Finally, system return to the normal operation. However, the
code is a little tricky. Before turning off the virtual memory, you must lock the
code jumping to physical address in cache. Otherwise, behaviors, after turning off
the cache, is unpredictable.

Gavin Guo

Signature · 2 lines
Regards,
Peter Teoh
#9 lore raw

http://infocenter.arm.com/help/topic/com.arm.doc.dui0056d/ch07s05s01.html

this provide the additional info that cache store virtual addresses, and since different processes may have same virtual address, but different physical address, u have to beware!

Quoted 56 lines · Peter Teoh

For ARM, MMU info can be found here:

http://infocenter.arm.com/help/topic/com.arm.doc.ddi0333h/Babbhigi.html

That is the theory behind MMU in ARM....but if u want the high level API, look ?under the arch/arm/mm directory lots of examples there. ? Otherwise u might try the following steps as proposed by "Marco Wang" - google for it. ? To quote:

phys_to_virt() only works with directly mapped physical address. I don't think using phys_to_virt() is the best idea, anyway. Usually you do this in several steps in a device driver:

1. Call request_mem_region() to request virtual memory region;
2. Call ioremap() to map physical address to virtual address;
3. Read/write mapped virtual address by using iowriteXX() / ioreadXX(), etc. Here XX can be 8, 16, or 32 for example, represents bit width.
4. Call iounmap() and release_mem_region() to release memory mapping;

Thanks,
Marco Wang

Quoted 9 lines · Micha M.
Quoted 8 lines · Gavin Guo
Quoted 7 lines

So maybe I have to explain some more. There is some code located in the pysical address space and I need to call it from a kernel module. The problem is, that the code must be run from that location it is stored (it contains absolute jumps). So I'd like to be able to run that code in that address space, or to "tell" the keeernel to ignore page faults/memory protection on a certain address range, so that I can jump there run the code and return to the caller (kernel module)

Quoted 17 lines
Quoted 1 line

What is the architecture do you use? ex: x86, arm, mips,...

ARM.

Quoted 12 lines

I know in some platform like andes, it is possible to turn off the virtual memory.
Then you can jump to the physical address. After doing what you want, turning on virtual memory again. Finally, system return to the normal operation. However, the
code is a little tricky. Before turning off the virtual memory, you must lock the
code jumping to physical address in cache. Otherwise, behaviors, after turning off
the cache, is unpredictable.

Gavin Guo

--
Regards,
Peter Teoh

Signature · 2 lines
Regards,
Peter Teoh