github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/cpvmm/vmm/test/rektest.cpp (about)

     1  /*
     2   * Copyright (c) 2013 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *     http://www.apache.org/licenses/LICENSE-2.0
     8   * Unless required by applicable law or agreed to in writing, software
     9   * distributed under the License is distributed on an "AS IS" BASIS,
    10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11   * See the License for the specific language governing permissions and
    12   * limitations under the License.
    13   */
    14  
    15  #include "stdio.h"
    16  #include "stdlib.h"
    17  
    18  typedef int INT32;
    19  typedef long long unsigned UINT64;
    20  typedef long long int INT64;
    21  
    22  
    23  INT32 hw_interlocked_add(INT32 volatile * addend, INT32 value)
    24  {
    25      asm volatile(
    26          "\tmovq     %[addend], %%rbx\n"
    27          "\tmovl     %[value], %%eax\n"
    28          "\tlock;    addl %%eax, (%%rbx)\n"
    29      : 
    30      : [addend] "p" (addend), [value] "r" (value)
    31      : "%eax", "%rbx");
    32      return *addend;
    33  }
    34  
    35  
    36  bool hw_scan_bit_backward( unsigned *bit_number_ptr, unsigned bitset )
    37  {
    38      bool ret = false;
    39  
    40      asm volatile(
    41          "\tbsrl %[bitset], %%eax\n"
    42          "\tmov %[bit_number_ptr], %%rbx\n"
    43          "\tmovl %%eax, (%%rbx)\n"
    44      : 
    45      : [bit_number_ptr] "p" (bit_number_ptr), [bitset] "g"(bitset)
    46      : "%eax", "%rbx");
    47      return bitset ? true: false;
    48  }
    49  
    50  INT32 hw_interlocked_assign(INT32 volatile * target, INT32 new_value)
    51  {
    52      asm volatile(
    53          "\tmovq     %[target], %%rbx\n"
    54          "\tmovl     %[new_value], %%eax\n"
    55          "\tlock;    xchgl %%eax, (%%rbx)\n"
    56      :
    57      : [new_value] "m" (new_value), [target] "r" (target)
    58      : "%eax", "%rbx");
    59      return *target;
    60  }
    61  
    62  INT32 hw_interlocked_compare_exchange(INT32 volatile * destination,
    63                                         INT32 exchange, INT32 comperand)
    64  {
    65      asm volatile(
    66          "\tmovl     %[exchange], %%eax\n"
    67          "\tmovl     %[comperand], %%ebx\n"
    68          "\tcmpxchgl %%ebx, %%edx\n"
    69          "\tmovq     %[destination], %%rbx\n"
    70          "\tmovl     %%edx, (%%rbx)\n"
    71      :
    72      : [exchange] "m" (exchange), [comperand] "m" (comperand),
    73        [destination] "m" (destination)
    74      :"%eax", "%ecx", "%edx", "%rbx");
    75      return *destination;
    76  }
    77  
    78  
    79  
    80  // CHECK(JLM)
    81  INT32 gcc_interlocked_compare_exchange(INT32 volatile * destination,
    82                                         INT32 exchange, INT32 comperand)
    83  {
    84      asm volatile(
    85          "\tmovq     %[destination], %%rbx\n"
    86          "\tmovl     %[exchange], %%eax\n"
    87          "\tmovl     %[comperand], %%ecx\n"
    88          "\tcmpxchgl %%ecx, %%eax\n"
    89          "\tmovl     %%eax, (%%rbx)\n"
    90      :
    91      : [exchange] "m" (exchange), [comperand] "m" (comperand),
    92        [destination] "m" (destination)
    93      :"%eax", "%ecx", "%rbx");
    94      return *destination;
    95  }
    96  
    97  INT64 gcc_interlocked_compare_exchange_64(INT64 volatile * destination,
    98              INT64 exchange, INT64 comperand)
    99  {
   100      asm volatile(
   101          "\tmovq     %[destination], %%rbx\n"
   102          "\tmovq     %[exchange], %%rax\n"
   103          "\tmovq     %[comperand], %%rcx\n"
   104          "\tcmpxchgq %%rcx, %%rax\n"
   105          "\tmovq     %%rax, (%%rbx)\n"
   106      :
   107      : [exchange] "m" (exchange), [comperand] "m" (comperand),
   108        [destination] "m" (destination)
   109      :"%rax", "%rcx", "%rbx");
   110      return *destination;
   111  }
   112  
   113  int main(int an, char** av)
   114  {
   115      unsigned a, b;
   116      unsigned* pa= &a;
   117      b= 0x00400;
   118      bool ret= hw_scan_bit_backward(pa, b);
   119      printf("Number: 0x%08x, %d; ret: %d\n", b, *pa, ret);
   120  
   121      int i, j, k, n;
   122      i= 5;
   123      j= 6;
   124      n= i; 
   125  
   126      printf("next test\n");
   127  
   128      k= hw_interlocked_add(&i, j);
   129      printf("%d %d, %d %d\n", n,j,i,k);
   130  
   131      i= 10;
   132      n= 10;
   133      k= hw_interlocked_assign(&i, 12);
   134      printf("orig %d, %d %d\n", n,i,k);
   135  
   136      n= 12;
   137      i= 11;
   138      j= 12;
   139      k= hw_interlocked_compare_exchange(&n, i, j);
   140      printf("%d %d <-- %d %d\n", k, n, i, j);
   141  
   142      n= 11;
   143      i= 11;
   144      j= 11;
   145      k= hw_interlocked_compare_exchange(&n, i, j);
   146      printf("%d %d <-- %d %d\n", k, n, i, j);
   147  
   148      return 0;
   149  }
   150  
   151