impossible constraint in 'asm' error - linux

I'm trying to cross-compile a c++ file with arm-linux-gnueabi-g++.:
make CROSS_COMPILE=arm-linux-gnueabi- all
But an error occurs:
arm-linux-gnueabi-g++ -O2 -c -oa32.o PMCTestA.cpp
In file included from PMCTest.h:54:0,
from PMCTestA.cpp:24:
PMCTestLinux.h: In member function ‘void CCounters::GetProcessorVendor()’:
PMCTestLinux.h:63:73: error: impossible constraint in ‘asm’
make: *** [a32.o] Error 1
And here is part of the code in PMCTestLinux.h from row 61:
static void Cpuid (int Output[4], int aa) {
int a, b, c, d;
__asm("cpuid" : "=a"(a),"=b"(b),"=c"(c),"=d"(d) : "a"(aa),"c"(0) : );
Output[0] = a;
Output[1] = b;
Output[2] = c;
Output[3] = d;
}
static inline void Serialize () {
// serialize CPU
__asm__ __volatile__ ( "xorl %%eax, %%eax \n cpuid " : : : "%eax","%ebx","%ecx","%edx" );
}
static inline int Readtsc() {
// read time stamp counter
int r;
__asm__ __volatile__ ( "rdtsc" : "=a"(r) : : "%edx");
return r;
}
static inline int Readpmc(int nPerfCtr) {
// read performance monitor counter number nPerfCtr
int r;
__asm__ __volatile__ ( "rdpmc" : "=a"(r) : "c"(nPerfCtr) : "%edx");
return r;
}
Does anyone know something about this error? I'm not sure if the problem is about the compiler. My system is Ubuntu 10.04 and the version of arm-linux-gnueabi-g++ is 4.7.3

Related

"too many memory references for mov" error when embedding assembly in C++

I wrote a program to check if I am working with virtual machine or not using assembly for linux. When I compile it, it shows the error "too many memory references for mov". How can I fix this? Can you guys help me check if my program is correct or not?
#include <stdio.h>
#include <iostream>
using namespace std;
bool IsInsideVMWare() {
bool rc = true;
try {
asm (
"push   edx;"
"push   ecx;"
"push   ebx;"
"mov    eax, 'VMXh';"
"mov    ebx, 0;" // any value but not the MAGIC VALUE
"mov    ecx, 10;" // get VMWare version
"mov    edx, 'VX';" // port number
"in     eax, dx;" // read port on return EAX returns the VERSION
"cmp    ebx, 'VMXh';" // is it a reply from VMWare?
"setz   [rc];" // set return value
"pop    ebx;"
"pop    ecx;"
"pop    edx;"
);
}
catch(const bool a) {
rc = false;
}
return rc;
}
int main() {
if (IsInsideVMWare()) {
cout << "VM Ware";
}
return 0;
}
You seem to have copied code intended for MSVC. I have converted to gcc inline assembly for you:
#include <stdint.h>
#include <iostream>
using namespace std;
bool IsInsideVMWare() {
bool rc = true;
try {
const uint32_t magic = 0x564D5868; // VMXh
const uint16_t port = 0x5658; // VX
uint32_t result, flag;
asm ("in %[port], %[result]"
: [result] "=a" (result), "=b" (flag)
: "0" (magic), "c" (10), [port] "d" (port));
rc = (flag == magic);
}
catch(const bool a) {
rc = false;
}
return rc;
}
int main() {
if (IsInsideVMWare()) {
cout << "VM Ware";
}
return 0;
}
Note that when not running under VMWare the in instruction requires privileges otherwise you get a segfault that the try/catch will not handle. As a workaround you can run as root and enable the privileges while hoping that accessing the vmware port will be harmless on your system:
#include <stdint.h>
#include <sys/io.h>
#include <iostream>
using namespace std;
bool IsInsideVMWare() {
const uint32_t magic = 0x564D5868; // VMXh
const uint16_t port = 0x5658; // VX
uint32_t result, flag;
iopl(3);
asm ("in %[port], %[result]"
: [result] "=a" (result), "=b" (flag)
: "0" (magic), "c" (10), [port] "d" (port));
return (flag == magic);
}
int main() {
if (IsInsideVMWare()) {
cout << "VM Ware";
}
return 0;
}
Depending on your requirements, it may be better to check the DMI product or vendor name, both of which contain "VMWare" on my system.

Where could I find the code of "sched_getcpu()"

Recently I'm using the function sched_getcpu() from the header file sched.h on Linux.
However, I'm wondering where could I find the source code of this function?
Thanks.
Under Linux, the sched_getcpu() function is a glibc wrapper to sys_getcpu() system call, which is architecture specific.
For the x86_64 architecture, it is defined under arch/x86/include/asm/vgtod.h as __getcpu() (tree 4.x):
#ifdef CONFIG_X86_64
#define VGETCPU_CPU_MASK 0xfff
static inline unsigned int __getcpu(void)
{
unsigned int p;
/*
* Load per CPU data from GDT. LSL is faster than RDTSCP and
* works on all CPUs. This is volatile so that it orders
* correctly wrt barrier() and to keep gcc from cleverly
* hoisting it out of the calling function.
*/
asm volatile ("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG));
return p;
}
#endif /* CONFIG_X86_64 */
Being this function called by __vdso_getcpu() declared in arch/entry/vdso/vgetcpu.c:
notrace long
__vdso_getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *unused)
{
unsigned int p;
p = __getcpu();
if (cpu)
*cpu = p & VGETCPU_CPU_MASK;
if (node)
*node = p >> 12;
return 0;
}
(See vDSO for details regarding what vdso prefix is).
EDIT 1: (in reply to arm code location)
ARM code location
It can be found in the arch/arm/include/asm/thread_info.h file:
static inline struct thread_info *current_thread_info(void)
{
return (struct thread_info *)
(current_stack_pointer & ~(THREAD_SIZE - 1));
}
This function is used by raw_smp_processor_id() that is defined in the file arch/arm/include/asm/smp.h as:
#define raw_smp_processor_id() (current_thread_info()->cpu)
And it's called by getcpu system call declared in the file kernel/sys.c:
SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep, struct getcpu_cache __user *, unused)
{
int err = 0;
int cpu = raw_smp_processor_id();
if (cpup)
err |= put_user(cpu, cpup);
if (nodep)
err |= put_user(cpu_to_node(cpu), nodep);
return err ? -EFAULT : 0;
}

pthreading in parallel computing

#include<stdio.h>
#include<math.h>
#include<pthread.h>
#include<stdlib.h>
long double x,fact[150],pwr[150],s[1];
int i,term;
void *Power(void *temp)
{
int k;
for(k=0;k<150;k++)
{
pwr[k] = pow(x,k);
//printf("%.2Lf\n",pwr[k]);
}
return pwr;
}
void *Fact(void *temp)
{
long double f;
int j;
fact[0] = 1.0;
for(term=1;term<150;term++)
{
f = 1.0;
for(j=term;j>0;j--)
f = f * j;
fact[term] = f;
//printf("%.2Lf\n",fact[term]);
}
return fact;
}
void *Exp(void *temp)
{
int t;
s[0] = 0;
for(t=0;t<150;t++)
s[0] = s[0] + (pwr[t] / fact[t]);
return s;
}
int main(void)
{
pthread_t thread1,thread2,thread3;
printf("Enter the value of x (between 0 to 100) (for calculating exp(x)) : ");
scanf("%Lf",&x);
printf("\nThreads creating.....\n");
pthread_create(&thread1,NULL,Power,NULL); //calling power function
pthread_create(&thread2,NULL,Fact,NULL); //calling factorial function
printf("Threads created\n");
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
printf("Master thread and terminated threads are joining\n");
printf("Result collected in Master thread\n");
pthread_create(&thread3,NULL,Exp,NULL);
pthread_join(thread3,NULL);
printf("\nValue of exp(%.2Lf) is : %Lf\n\n",x,s[0]);
exit(1);
}
I was trying to run the above program in linux ubuntu. It is giving following errors
parallelcomp.cpp:(.text+0x1ec): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x207): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x222): undefined reference to `pthread_join'
parallelcomp.cpp:(.text+0x233): undefined reference to `pthread_join'
parallelcomp.cpp:(.text+0x262): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x273): undefined reference to `pthread_join'
The error is mostprobably due to linking binary with pthreads.
Is there any command in ubuntu terminal whcih can solve this problem.?
I have tried with several commands given in this community forum, non of them is helpful.
Is there anyone who would like to help me?
I am also very new to Linux ubuntu.
Any kind of suggestion is appreciable.
How to include libpthread ?
When I am putting the following command, gcc -pthread -o term term.c, in terminalwe i get the following error: Command line option 'p' [from -pthread] is not known.
Please try the following -lpthread. Which version of gcc are you using?
Why do I get "undefined reference" errors even when I include the right header files?
While compiling add link to pthread library -lpthread

VC warning 4365 seems to be inconsistent

My question is: why does VC emit warning 4365 for only one of the commented lines below, and not the other?
#pragma warning(1: 4365)
void test1(const unsigned short) {}
unsigned short test2() { return 0; }
int main()
{
const unsigned short a = 0;
const unsigned short b = 0;
test1(a + b); // This line gives no warning
test1(test2() + b); // This line gives C4365
return 0;
}
Tested under VS2010 and VS2012 Express.
For reference, the full warning text is this:
warning C4365: 'argument' : conversion from 'int' to 'const unsigned short', signed/unsigned mismatch
Using Clang 3.3 (through Clang-Win32 and ClangVSx), no warnings are reported in this code (except of course the unknown pragma).

Passing struct pointer als parameter for pthread function

Hello I'm fallowing a tutorial for pthreading, but something went wrong down the road since can't make the reference to my struct passable by
pthread_create(...,(void* stuctName)) , I'm looking for some advices or fixes since I have no idea where or what I messed up...
code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <math.h>
struct dataBlock{
struct node *root;
int listSize;
int forIndex;
};
struct node { // std linked list node
int value;
int worker;
struct node *next;
};
int slots = 3; // only 3 threads are allowed to access the list
int availableCheck(){ // check if thread can acces the list
if(slots < 3) return 0;
else return -1;
}
pthread_mutex_t mutp = PTHREAD_MUTEX_INITIALIZER; //condvar mutex
pthread_cond_t condvar = PTHREAD_COND_INITIALIZER; //condvar
void * worker( void *data ){ //WORKER FUNCTION
printf( "* Thread start: ^\n");
struct dataBlock *inData = (struct dataBlock *) data;
struct node *root = data->root;
int listSize = data->listSize;
int forIndex = data ->forIndex;
// printf( " * I am %li _ worker # %li : \n", forIndex, pthread_self() );
pthread_mutex_lock( &mutp );
if(availableCheck() < 0){
printf( " ^^^ List not available yet... \n" );
pthread_cond_wait( &condvar, &mutp );
}
// printf( "* Got data: %lu \n", index );
pthread_cond_signal( &condvar ); //
pthread_mutex_unlock( &mutp );
return NULL;
}
int main( int argc, char *argv[] ){
if ( argc != 3 ){
printf( "Programm must be called with \n NR of elements and NR of workers! \n " );
exit( 1 );
int i;
struct node *root;
struct node *iterator;
//prepare list for task
int listSize = atoi(argv[1]);
int nrWorkers = atoi(argv[2]);
root = malloc(sizeof( struct node) );
root->value = rand() % 100;
root->worker = 0;
iterator = root;
for( i=1; i<listSize; i++ ){
iterator->next = malloc(sizeof(struct node));
iterator = iterator->next;
iterator->value = rand() % 100;
iterator->worker = i % nrWorkers;
printf("node #%d worker: %d value: %d\n", i, iterator->worker,iterator->value);
}
// Create all threads to parse the link list
int ret, *id;
printf("workersInput: %d\n",nrWorkers);
pthread_t w_thread;
pthread_t* w_threads = malloc(nrWorkers * sizeof(w_thread));
struct dataBlock *data = malloc(sizeof(struct dataBlock));
data->root = root;
data->listSize = listSize;
for( i=0; i < nrWorkers; i++ ){ // CREATING THREADS
data->forIndex = i;
ret = pthread_create ( &w_threads[i], NULL, worker, (void *) data );
if( ret ) {
perror("Thread creation fail");
exit(2);
}
}
for ( i = 0; i < nrWorkers; i++){
pthread_join(w_threads[i],NULL);
}
free(root);
free(iterator);
return 0;
}
Compiling(Wall flag)
s.c: In function ‘worker’:
s.c:32:26: warning: dereferencing ‘void *’ pointer [enabled by default]
s.c:32:26: error: request for member ‘root’ in something not a structure or union
s.c:33:22: warning: dereferencing ‘void *’ pointer [enabled by default]
s.c:33:22: error: request for member ‘listSize’ in something not a structure or union
s.c:34:22: warning: dereferencing ‘void *’ pointer [enabled by default]
s.c:34:22: error: request for member ‘forIndex’ in something not a structure or union
s.c:34:6: warning: unused variable ‘forIndex’ [-Wunused-variable]
s.c:33:6: warning: unused variable ‘listSize’ [-Wunused-variable]
s.c:32:15: warning: unused variable ‘root’ [-Wunused-variable]
s.c:31:20: warning: unused variable ‘inData’ [-Wunused-variable]
s.c: In function ‘main’:
s.c:81:12: warning: variable ‘id’ set but not used [-Wunused-but-set-variable]
Use inData->root instead of data->root etc... (or cast explicitly always your data) since data is a void* pointer, that is a pointer to some unspecified data type.

Resources