Skip to contentSkip to conversation

Xen-aware kernel module

DISCUSSION Steven Y. Ko 4 participants 5 messages Nov 21, 2007

lore
Share & tools
lore
#0 lore raw

Hello,

(I'm not sure if this is an appropriate place to ask this question. I apologize, if it's not)

I'm trying to write a kernel module, that's sort of Xen-aware (or virtualisation-aware). I say "sort of", because what I actually need is checking the ring. Specifically, in my kernel module, I want to check which ring the code is running on (ring0, ring1, or even ring 3!), and behave differently. Does Xen provide any interface to do it? or is there any way to do it in a kernel module? Thanks for any help.

Regards,
Steve

#1 lore raw
Quoted 11 lines · Steven Y. Ko

Hello,

(I'm not sure if this is an appropriate place to ask this question. I apologize, if it's not)

I'm trying to write a kernel module, that's sort of Xen-aware (or virtualisation-aware). I say "sort of", because what I actually need is checking the ring. Specifically, in my kernel module, I want to check which ring the code is running on (ring0, ring1, or even ring 3!), and behave differently. Does Xen provide any interface to do it? or is there any way to do it in a kernel module? Thanks for any help.

well, if you had a xen interface to check it, then you already know you're not running on ring0, right?

i suppose you will not consider HVM in your equation, because the ring idea makes only sense with pv. so, the solution may be:

#ifdef CONFIG_X86_XEN

# ifdef CONFIG_X86_32
ring = 1;
# else
ring = 3;
# endif
#else
ring = 0;
#endif

amounts to a minumum of runtime overhead <:). i forgot: add tests for XENFEAT_supervisor_mode_kernel, but I believe it that never really got into vogue (someone correct me if i'm wrong).

CPL is stored only internally. If it should look smarter, one might consider following the %cs selector, but strictly speaking this isn't bulletproof as the descriptor might have altered in the meanwhile (on linux, it's usually safe). There may be some alternatives I'm admittedly rather unaware of.

checking privilege levels that way will, if at all, only work on PV. full virtualization (because you mentioned 'virtualization-aware' vs. xen-only) will do its best to assure you you're running on cpl 0. similar applies to VT-x root vs. non-root mode.

hth,
daniel

Signature · 5 lines
Daniel Stodden
LRR     -      Lehrstuhl für Rechnertechnik und Rechnerorganisation
Institut für Informatik der TU München             D-85748 Garching
http://www.lrr.in.tum.de/~stodden         mailto:[email protected]
PGP Fingerprint: F5A4 1575 4C56 E26A 0B33  3D80 457E 82AE B0D8 735B
#2 lore raw
In reply to Steven Y. Ko
Quoted 11 lines

Hello,

(I'm not sure if this is an appropriate place to ask this question. I apologize, if it's not)

I'm trying to write a kernel module, that's sort of Xen-aware (or virtualisation-aware). I say "sort of", because what I actually need is checking the ring. Specifically, in my kernel module, I want to check which ring the code is running on (ring0, ring1, or even ring 3!), and behave differently. Does Xen provide any interface to do it? or is there any way to do it in a kernel module? Thanks for any help.

There may or may not be a CPU instruction to tell you what RING you are running in.

As for telling if you are running under Xen, have a look at /unmodified_drivers/linux-2.6/platform-pci/platform-pci.c in the function init_hypercall_stubs. The first thing it does is execute a CPUID instruction. If Xen is running, a signature will be encoded into EBX, ECX, and EDX. You might be able to make use of that?

James

#3 lore raw
In reply to Steven Y. Ko

Since this code will be x86-specific anyway, you can use inline asm:

unsigned long ring;
asm ( "push %cs; pop %0; and $3,%0" : "=r" (ring) );

Or perhaps just look at the KERNEL_CS macro.

-- Keir

Quoted 19 lines · Steven Y. Ko

Hello,

(I'm not sure if this is an appropriate place to ask this question. I apologize, if it's not)

I'm trying to write a kernel module, that's sort of Xen-aware (or virtualisation-aware). I say "sort of", because what I actually need is checking the ring. Specifically, in my kernel module, I want to check which ring the code is running on (ring0, ring1, or even ring 3!), and behave differently. Does Xen provide any interface to do it? or is there any way to do it in a kernel module? Thanks for any help.

Regards,
Steve

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#4 lore raw

Thank you all for the great tips!

- Steve

Quoted 34 lines · Keir Fraser

Since this code will be x86-specific anyway, you can use inline asm:

unsigned long ring;
asm ( "push %cs; pop %0; and $3,%0" : "=r" (ring) );

Or perhaps just look at the KERNEL_CS macro.

-- Keir

Quoted 19 lines · Steven Y. Ko

Hello,

(I'm not sure if this is an appropriate place to ask this question. I apologize, if it's not)

I'm trying to write a kernel module, that's sort of Xen-aware (or virtualisation-aware). I say "sort of", because what I actually need is checking the ring. Specifically, in my kernel module, I want to check which ring the code is running on (ring0, ring1, or even ring 3!), and behave differently. Does Xen provide any interface to do it? or is there any way to do it in a kernel module? Thanks for any help.

Regards,
Steve

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel