Re: Enable SMP support

2018-06-15 Thread Almudena Garcia
Yes, I saw this.

When I test It in Linux, I used a dual core computer. But the snippet
returns "8" number.

Now I know the reason ;)

2018-06-15 19:25 GMT+02:00 Richard Braun :

> On Fri, Jun 15, 2018 at 07:18:55PM +0200, Richard Braun wrote:
> > On Fri, Jun 15, 2018 at 06:27:21PM +0200, Almudena Garcia wrote:
> > > I'm trying to define the cpu_number() in multiprocessor.
> > >
> > > To do this, I tried to use CPUID assembly x86 instruction, to get the
> CPU
> > > SMP number.
> > > The function, in C, is this:
> > >
> > > static inline char smp_processor_id(void) {
> > >   char apic_id = 0;
> > >   asm("mov $1, %%eax\n\t"
> > >   "cpuid\n\t"
> > >   "mov %%bh, %0\n\t" : "=g" (apic_id));
> > >   return apic_id;
> > > }
> > >
> > > In Linux, after executing this in a test source, It returns '8'
> > >
> > > But, when I try to execute It in Hurd, It shows a segmentation fault.
> > >
> > > I attach the test source file
> > >
> > >
> > > Can you help me?
>
> Also note here that you're confusing the APIC ID, a device identifier at
> the hardware level, with the CPU ID, a processor identifier at the kernel
> level. On some machines, the APIC ID may be higher than or equal to the
> maximum number of processor installed.
>
> --
> Richard Braun
>


Re: Enable SMP support

2018-06-15 Thread Richard Braun
On Fri, Jun 15, 2018 at 07:18:55PM +0200, Richard Braun wrote:
> On Fri, Jun 15, 2018 at 06:27:21PM +0200, Almudena Garcia wrote:
> > I'm trying to define the cpu_number() in multiprocessor.
> > 
> > To do this, I tried to use CPUID assembly x86 instruction, to get the CPU
> > SMP number.
> > The function, in C, is this:
> > 
> > static inline char smp_processor_id(void) {
> >   char apic_id = 0;
> >   asm("mov $1, %%eax\n\t"
> >   "cpuid\n\t"
> >   "mov %%bh, %0\n\t" : "=g" (apic_id));
> >   return apic_id;
> > }
> > 
> > In Linux, after executing this in a test source, It returns '8'
> > 
> > But, when I try to execute It in Hurd, It shows a segmentation fault.
> > 
> > I attach the test source file
> > 
> > 
> > Can you help me?

Also note here that you're confusing the APIC ID, a device identifier at
the hardware level, with the CPU ID, a processor identifier at the kernel
level. On some machines, the APIC ID may be higher than or equal to the
maximum number of processor installed.

-- 
Richard Braun



Re: Enable SMP support

2018-06-15 Thread Richard Braun
On Fri, Jun 15, 2018 at 06:27:21PM +0200, Almudena Garcia wrote:
> I'm trying to define the cpu_number() in multiprocessor.
> 
> To do this, I tried to use CPUID assembly x86 instruction, to get the CPU
> SMP number.
> The function, in C, is this:
> 
> static inline char smp_processor_id(void) {
>   char apic_id = 0;
>   asm("mov $1, %%eax\n\t"
>   "cpuid\n\t"
>   "mov %%bh, %0\n\t" : "=g" (apic_id));
>   return apic_id;
> }
> 
> In Linux, after executing this in a test source, It returns '8'
> 
> But, when I try to execute It in Hurd, It shows a segmentation fault.
> 
> I attach the test source file
> 
> 
> Can you help me?

You said in your initial message that you were willing to start all this
at your own risk. Well this is it, and it's only the very beginning.

This inline assembly snippet is invalid because it doesn't tell the
compiler that it uses registers, which may conflict with the code
generated by the compiler. Read the GCC extended inline assembly
documentation carefully, and especially the machine constraints [1]
for the x86 family. The idea is to create an eax variable with the
value 1 in C, pass that to the inline assembly expression with the
appropriate constraint, and do something similar for the returned
value in ebx, and then extract bh from it.

-- 
Richard Braun

[1] 
https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Machine-Constraints.html#Machine-Constraints



Re: Enable SMP support

2018-06-15 Thread Almudena Garcia
I'm trying to define the cpu_number() in multiprocessor.

To do this, I tried to use CPUID assembly x86 instruction, to get the CPU
SMP number.
The function, in C, is this:

static inline char smp_processor_id(void) {
  char apic_id = 0;
  asm("mov $1, %%eax\n\t"
  "cpuid\n\t"
  "mov %%bh, %0\n\t" : "=g" (apic_id));
  return apic_id;
}

In Linux, after executing this in a test source, It returns '8'

But, when I try to execute It in Hurd, It shows a segmentation fault.

I attach the test source file


Can you help me?


2018-06-15 1:52 GMT+02:00 Almudena Garcia :

> Ok. I understand you. But then... how can I to define cpu_number()
> function in multiprocessor? It isn't clear
>
> 2018-06-15 1:30 GMT+02:00 Amos Jeffries :
>
>> On 15/06/18 10:31, Almudena Garcia wrote:
>> > Yes, I concluded the same a few hours ago. I tried to edit
>> > kern/cpu_number.h, to add a new definition with many cores, at this form
>> >
>> > int master_cpu; /* 'master' processor - keeps time */
>> >
>> > #if (NCPUS == 1)
>> > /* cpu number is always 0 on a single processor system */
>> > #define cpu_number()(0)
>> >
>> > #define CPU_L1_SIZE (1 << CPU_L1_SHIFT)
>> >
>> > #else
>> > #define cpu_number() (NCPUS)
>> >
>>
>> I may be wrong, but that does not seem correct.
>>
>> The man page for cpu_number() state it to be "an unique CPU
>> identification number for the CPU that this thread is running on". So it
>> clearly can change at runtime if there are 2+ CPUs on the machine.
>>
>> Whereas NCPUS is being used by the precompiler as an integer with fixed
>> value that can be known at pre-compile time. Implied by the "#if NCPUS >
>> 1" statement which this version of the code omits.
>>
>>
>> AYJ
>>
>>
>
#include 

static inline char smp_processor_id(void) {
  char apic_id = 0;
  asm("mov $1, %%eax\n\t"
  "cpuid\n\t"
  "mov %%bh, %0\n\t" : "=g" (apic_id));
  return apic_id;
}

int main (void) {
  printf("%d\n", smp_processor_id());

  return 0;
}