kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: dhylands@gmail.com (Dave Hylands)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Identifying whether a user-process or kernel-thread execution takes place by looking at CPU registers
Date: Thu, 12 May 2011 18:55:16 -0700	[thread overview]
Message-ID: <BANLkTin=WTMD+BCuD_XwD_fP0ADjNKHuBw@mail.gmail.com> (raw)
In-Reply-To: <BANLkTinfr5U4iBqLzhFsxcGETfFXd6cpiw@mail.gmail.com>

Hi guys,

On Thu, May 12, 2011 at 4:53 AM, Mulyadi Santosa
<mulyadi.santosa@gmail.com> wrote:
> Hi....
>
> On Thu, May 12, 2011 at 17:55, limp <johnkyr83@hotmail.com> wrote:
>> I was wondering if any other CPU register (apart from CR3) can indicate if a
>> user-process or a kernel thread under it (and which one) is executed. Is it
>> possible to know such a thing *only* by looking at CPU registers?
>
> one thing you can use is by looking at so called CPL (Current
> Privilege level) and check it whether it is 0. According to
> http://en.wikipedia.org/wiki/X86_memory_segmentation, CPL is the lower
> 2 bits in CS.
>
> However, you need to watch it continously, because user space apps
> could switch to CPL=0 (which denotes kernel mode, where CPL=3 denotes
> user mode) in the case of system call etc.
>
> perhaps better is by looking at the address of mm. However, to do
> this, you need to check starting from its task_struct, which is mapped
> in its kernel stack in x86 AFAIK. In other arch such as ARM, AFAIK
> task_struct could be simply derived from certain register.

On the ARM, you can derive the task_struct from the stack pointer.

>From kernel context, you can just use "current" which is a pointer to
the currently running task.

If you want details about how to determine the task_struct from SP, I
can get into that, although it's subject to change. Using "current" it
the normal technique.

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.davehylands.com

  reply	other threads:[~2011-05-13  1:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-12 10:55 Identifying whether a user-process or kernel-thread execution takes place by looking at CPU registers limp
2011-05-12 11:53 ` Mulyadi Santosa
2011-05-13  1:55   ` Dave Hylands [this message]
2011-05-14 17:55     ` limp
2011-05-14 19:12       ` Dave Hylands

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='BANLkTin=WTMD+BCuD_XwD_fP0ADjNKHuBw@mail.gmail.com' \
    --to=dhylands@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).