1

I found kern/processor.h files that include current_processor().

But I cannot use current_processor() even i include kern/processor.h file.

Is there any methods to get current processor id?

Add 030420 : I need any methods that get current processor id and methods should be possible to used in KEXT. current_processor(), cpu_number() doesn't work on KEXT.

이유찬
  • 43
  • 4

1 Answers1

0

The following function is declared in <kern/cpu_number.h>:

extern int  cpu_number(void);

and returns the index of the CPU on which the code is currently executing.

Please note that this is in the unsupported KPI however, so you need to link against com.apple.kpi.unsupported.

Also note that the result will be meaningless unless preemption is disabled, which is of course normally not the case, only when a spinlock is held, or when running in a primary interrupt handler. Preemption being enabled means that the running thread can be rescheduled at any time, so by the time your code uses the CPU number it obtained by calling the above function, it may already have been rescheduled to run on a different CPU.

pmdj
  • 22,018
  • 3
  • 52
  • 103
  • i'm sorry but it isn't helpful to me. i want to make per_cpu mechanism so current processor number is needed – 이유찬 Mar 03 '20 at 10:38
  • @이유찬 Well, `cpu_number()` returns the current CPU number, so this is just as good as `current_processor()` except you can actually use it. If you actually need something other than the current CPU ID you will need to update your question. I've updated my answer to perhaps be a little clearer. – pmdj Mar 03 '20 at 10:46
  • @이유찬 Why do you say that `cpu_number()` does not work in a kext? As far as I'm aware it should work just fine. – pmdj Mar 03 '20 at 19:42
  • Thank you for your information! cpu_num is successfully worked. – 이유찬 Mar 04 '20 at 15:07
  • The reason why i told cpu_number doesn't work is when include header evoke errors.(can't find header file) Anyway, i'm very very thankful that you spend so much times to my problem. – 이유찬 Mar 04 '20 at 15:11
  • @이유찬 Hi, How did you end up invoking the cpu_numbers? As you mentioned, it is not possible to include the kern/cpu_numbers header in the kext module. – ruach Apr 26 '21 at 02:17
  • @ruach Simply declare the function in your own code – pmdj Apr 26 '21 at 05:12
  • @pmdj After just exporting that function in my kext code and compile, it is successfully compiled. However, at the load time it rejects kextloading because of unresolved symbol – ruach Apr 26 '21 at 09:05
  • @ruach Have you included the com.apple.kpi.unsupported KPI in your dependencies? What OS version and architecture? – pmdj Apr 26 '21 at 09:07
  • @pmdj Yes I did. Below is the error message Failed to bind '_cpu_number' in 'kext' (at offset 0x0 in __DATA_CONST, __got) as could not find a kext which exports this symbol. Macos version is 11.4 Beta (20F5046g) on M1 machine – ruach Apr 26 '21 at 09:12
  • @ruach __DATA_CONST seems odd, perhaps post a new question with all the details, comments aren’t really suitable for this sort of thing. It’s possible Apple has removed it from the arm64 KPI though, I haven’t checked. – pmdj Apr 26 '21 at 09:15
  • Thanks I will post another question and paste link here. I appreciate it – ruach Apr 26 '21 at 09:16
  • 1
    @pmdj Could you take a look at the below question? Thanks a lot! https://stackoverflow.com/questions/67264758/how-to-access-unexported-symbol-from-kext-ios – ruach Apr 26 '21 at 10:15
  • Done. As it turns out, the problem is indeed that you're trying to access the function on `arm64`, where it's not exported. https://stackoverflow.com/a/67266131/48660 – pmdj Apr 26 '21 at 11:53