Ejemplo n.º 1
0
int __kprobes hw_breakpoint_handler(struct die_args *args)
{
	int rc = NOTIFY_STOP;
	struct perf_event *bp;
	struct pt_regs *regs = args->regs;
	int stepped = 1;
	struct arch_hw_breakpoint *info;
	unsigned int instr;
	unsigned long dar = regs->dar;

	
	set_dabr(0);

	rcu_read_lock();

	bp = __get_cpu_var(bp_per_reg);
	if (!bp)
		goto out;
	info = counter_arch_bp(bp);

	if (bp->overflow_handler == ptrace_triggered) {
		perf_bp_event(bp, regs);
		rc = NOTIFY_DONE;
		goto out;
	}

	info->extraneous_interrupt = !((bp->attr.bp_addr <= dar) &&
			(dar - bp->attr.bp_addr < bp->attr.bp_len));

	
	if (user_mode(regs)) {
		bp->ctx->task->thread.last_hit_ubp = bp;
		regs->msr |= MSR_SE;
		goto out;
	}

	stepped = 0;
	instr = 0;
	if (!__get_user_inatomic(instr, (unsigned int *) regs->nip))
		stepped = emulate_step(regs, instr);

	if (!stepped) {
		WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
			"0x%lx will be disabled.", info->address);
		perf_event_disable(bp);
		goto out;
	}
	if (!info->extraneous_interrupt)
		perf_bp_event(bp, regs);

	set_dabr(info->address | info->type | DABR_TRANSLATION);
out:
	rcu_read_unlock();
	return rc;
}
Ejemplo n.º 2
0
/*
 * Called after single-stepping.  p->addr is the address of the
 * instruction whose first byte has been replaced by the "breakpoint"
 * instruction.  To avoid the SMP problems that can occur when we
 * temporarily put back the original opcode to single-step, we
 * single-stepped a copy of the instruction.  The address of this
 * copy is p->ainsn.insn.
 */
static void resume_execution(struct kprobe *p, struct pt_regs *regs)
{
	int ret;

	regs->nip = (unsigned long)p->addr;
	ret = emulate_step(regs, p->ainsn.insn[0]);
	if (ret == 0)
		regs->nip = (unsigned long)p->addr + 4;

	regs->msr &= ~MSR_SE;
}
Ejemplo n.º 3
0
/*
 * See if the instruction can be emulated.
 * Returns true if instruction was emulated, false otherwise.
 */
bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
{
	int ret;

	/*
	 * emulate_step() returns 1 if the insn was successfully emulated.
	 * For all other cases, we need to single-step in hardware.
	 */
	ret = emulate_step(regs, auprobe->ainsn);
	if (ret > 0)
		return true;

	return false;
}
Ejemplo n.º 4
0
/*
 * Handle debug exception notifications.
 */
int __kprobes hw_breakpoint_handler(struct die_args *args)
{
	int rc = NOTIFY_STOP;
	struct perf_event *bp;
	struct pt_regs *regs = args->regs;
	int stepped = 1;
	struct arch_hw_breakpoint *info;
	unsigned int instr;
	unsigned long dar = regs->dar;

	/* Disable breakpoints during exception handling */
	set_dabr(0, 0);

	/*
	 * The counter may be concurrently released but that can only
	 * occur from a call_rcu() path. We can then safely fetch
	 * the breakpoint, use its callback, touch its counter
	 * while we are in an rcu_read_lock() path.
	 */
	rcu_read_lock();

	bp = __get_cpu_var(bp_per_reg);
	if (!bp)
		goto out;
	info = counter_arch_bp(bp);

	/*
	 * Return early after invoking user-callback function without restoring
	 * DABR if the breakpoint is from ptrace which always operates in
	 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
	 * generated in do_dabr().
	 */
	if (bp->overflow_handler == ptrace_triggered) {
		perf_bp_event(bp, regs);
		rc = NOTIFY_DONE;
		goto out;
	}

	/*
	 * Verify if dar lies within the address range occupied by the symbol
	 * being watched to filter extraneous exceptions.  If it doesn't,
	 * we still need to single-step the instruction, but we don't
	 * generate an event.
	 */
	info->extraneous_interrupt = !((bp->attr.bp_addr <= dar) &&
			(dar - bp->attr.bp_addr < bp->attr.bp_len));

	/* Do not emulate user-space instructions, instead single-step them */
	if (user_mode(regs)) {
		current->thread.last_hit_ubp = bp;
		regs->msr |= MSR_SE;
		goto out;
	}

	stepped = 0;
	instr = 0;
	if (!__get_user_inatomic(instr, (unsigned int *) regs->nip))
		stepped = emulate_step(regs, instr);

	/*
	 * emulate_step() could not execute it. We've failed in reliably
	 * handling the hw-breakpoint. Unregister it and throw a warning
	 * message to let the user know about it.
	 */
	if (!stepped) {
		WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
			"0x%lx will be disabled.", info->address);
		perf_event_disable(bp);
		goto out;
	}
	/*
	 * As a policy, the callback is invoked in a 'trigger-after-execute'
	 * fashion
	 */
	if (!info->extraneous_interrupt)
		perf_bp_event(bp, regs);

	set_dabr(info->address | info->type | DABR_TRANSLATION, info->dabrx);
out:
	rcu_read_unlock();
	return rc;
}
Ejemplo n.º 5
0
int main (int argc, char** argv)
{
  StackDescription_t stkd ;
  Analysis_t         analysis ;
  Output_t           output ;
  ThermalData_t      tdata ;

  Error_t error ;

  struct timespec t1,t2;
  struct timespec res;
  res.tv_sec=0;
  res.tv_nsec=0;
  Quantity_t server_port ;

  Socket_t server_socket, client_socket ;

  NetworkMessage_t request, reply , tmapReply;

  bool headers = false ;

    /* Checks if all arguments are there **************************************/

  if (argc != 3)
  {
    fprintf (stderr, "Usage: \"%s file.stk server_port\n", argv[0]) ;

    return EXIT_FAILURE ;
  }

  server_port = atoi (argv[2]) ;

  /* Parses stack file (fills stack descrition and analysis) ****************/

  fprintf (stdout, "Preparing stk data ... ") ; fflush (stdout) ;

  stack_description_init (&stkd) ;
  analysis_init          (&analysis) ;
  output_init            (&output) ;
  
    error = parse_stack_description_file (argv[1], &stkd, &analysis, &output) ;

  /* Initialise the compression related data ********************************/

  Quantity_t nflpel_ = get_total_number_of_floorplan_elements (&stkd) ;

  float hist_table_d[nflpel_][HIST_TABLE_SIZE];
  
  int tail_d[nflpel_];
  
  for (unsigned int i = 0; i < nflpel_; i++)
  {
    tail_d[i] = 0;
  
    for (int j = 0; j < HIST_TABLE_SIZE; j++)
      hist_table_d[i][j] = -1.0;
  }

  for(int i=0;i<MAXRES;i++)
    for(int j=0;j<MAXROWS;j++)
      for(int k=0;k<MAXCOLS;k++)
        for(int l=0;l<HIST_TABLE_SIZE;l++)
          histTableMap[i][j][k][l] = -1.0;
  int compressionUsed;



  if (error != TDICE_SUCCESS)    return EXIT_FAILURE ;

  if (analysis.AnalysisType != TDICE_ANALYSIS_TYPE_TRANSIENT)
  {
    fprintf (stderr, "only transient analysis!\n") ;

    goto wrong_analysis_error ;
  }

  fprintf (stdout, "done !\n") ;

  /* Prepares thermal data **************************************************/

  fprintf (stdout, "Preparing thermal data ... ") ; fflush (stdout) ;

  thermal_data_init (&tdata) ;

  error = thermal_data_build

    (&tdata, &stkd.StackElements, stkd.Dimensions, &analysis) ;


  if (error != TDICE_SUCCESS)    goto ftd_error ;

  fprintf (stdout, "done !\n") ;

  /* Creates socket *********************************************************/

  fprintf (stdout, "Creating socket ... ") ; fflush (stdout) ;

  socket_init (&server_socket) ;

  error = open_server_socket (&server_socket, server_port) ;



  if (error != TDICE_SUCCESS)    goto socket_error ;

  fprintf (stdout, "done !\n") ;

  /* Waits for a client to connect ******************************************/

  fprintf (stdout, "Waiting for client ... ") ; fflush (stdout) ;

  socket_init (&client_socket) ;

  error = wait_for_client (&server_socket, &client_socket) ;

  if (error != TDICE_SUCCESS)    goto wait_error ;

  fprintf (stdout, "done !\n") ;

  //float timeForTmap = 0.0;
  /* Runs the simlation *****************************************************/

  do
  {
    network_message_init (&request) ;

    receive_message_from_socket (&client_socket, &request) ;

    switch (*request.Type)
    {

      /**********************************************************************/
      case TDICE_COMPRESSION_USED :
        {
          extract_message_word (&request, &compressionUsed,0);
          break;
        }
      case TDICE_EXIT_SIMULATION :
        {
          network_message_destroy (&request) ;

          goto quit ;
        }

        /**********************************************************************/

      case TDICE_RESET_THERMAL_STATE :
        {
          reset_thermal_state (&tdata, &analysis) ;

          break ;
        }

        /**********************************************************************/

      case TDICE_TOTAL_NUMBER_OF_FLOORPLAN_ELEMENTS :
        {
          network_message_init (&reply) ;

          build_message_head   (&reply, TDICE_TOTAL_NUMBER_OF_FLOORPLAN_ELEMENTS) ;

          Quantity_t nflpel = get_total_number_of_floorplan_elements (&stkd) ;

          insert_message_word (&reply, &nflpel) ;

          send_message_to_socket (&client_socket, &reply) ;

          network_message_destroy (&reply) ;

          break ;
        }

        /**********************************************************************/

      case TDICE_INSERT_POWERS_AND_SIMULATE_SLOT :
        {
          Quantity_t nflpel, index ;

          PowersQueue_t queue ;

          powers_queue_init (&queue) ;

          extract_message_word (&request, &nflpel, 0) ;

          powers_queue_build (&queue, nflpel) ;
          if(compressionUsed)
          {
            unsigned int tmp[3];
            int ret=-1;
            unsigned int compressedWord;
            float power=0.0;
            int readIndex=1;
            index=1;
            initTmp(tmp);
            extract_message_word (&request, &compressedWord,readIndex);
            while(index<=nflpel)
            {
              ret=getNextDecompressedWord(compressedWord,tmp,0);
              switch(ret)
              {
                case WI_DC:
                case WC_DC: 
                  switch(tmp[0])
                  {
                    case 0:
                      power=(int)hist_table_d[index-1][tmp[1]]+(hist_table_d[index-1][tmp[2]]-(int)hist_table_d[index-1][tmp[2]]);
                      break; 
                    case 1:
                      power=tmp[1]+(hist_table_d[index-1][tmp[2]]-(int)hist_table_d[index-1][tmp[2]]);
                      hist_table_d[index-1][tail_d[index-1]]=power;
                      tail_d[index-1]=(tail_d[index-1]+1)%HIST_TABLE_SIZE;
                      break;
                    case 2:
                      power=(int)hist_table_d[index-1][tmp[1]]+((float)tmp[2])/(pow(10,DECIMAL_DIGITS));
                      hist_table_d[index-1][tail_d[index-1]]=power;
                      tail_d[index-1]=(tail_d[index-1]+1)%HIST_TABLE_SIZE;
                      break;
                    case 3:
                      power=tmp[1]+((float)tmp[2])/(pow(10,DECIMAL_DIGITS));
                      hist_table_d[index-1][tail_d[index-1]]=power;
                      tail_d[index-1]=(tail_d[index-1]+1)%HIST_TABLE_SIZE;
                      break;
                  }
                  put_into_powers_queue(&queue,power);
                  index++;
                  initTmp(tmp);
                  if(ret==WC_DC)
                  {
                    readIndex++;
                    extract_message_word (&request, &compressedWord,readIndex);
                  }
                  break;
                case WC_DI:
                  readIndex++;
                  extract_message_word (&request, &compressedWord,readIndex);
                  break;
                default:
                  break;
              }
            }
            ret=getNextDecompressedWord(compressedWord,tmp,1);
          }
          /* NO COMPRESSION *************************************/
          else 
          {
            for (index = 1, nflpel++ ; index != nflpel ; index++)
            {
              float power_value ;

              extract_message_word (&request, &power_value, index) ;

              put_into_powers_queue (&queue, power_value) ;
            }

          }
          error = insert_power_values (&tdata.PowerGrid, &queue) ;

          if (error != TDICE_SUCCESS)
          {
            fprintf (stderr, "error: insert power values\n") ;

            powers_queue_destroy (&queue) ;

            goto sim_error ;
          }

          powers_queue_destroy (&queue) ;

          network_message_init (&reply) ;
          build_message_head   (&reply, TDICE_INSERT_POWERS_AND_SIMULATE_SLOT) ;

          SimResult_t result = emulate_slot

            (&tdata, stkd.Dimensions, &analysis) ;

          insert_message_word (&reply, &result) ;

          send_message_to_socket (&client_socket, &reply) ;

          network_message_destroy (&reply) ;

          if (result != TDICE_SLOT_DONE)
          {
            fprintf (stderr, "error %d: emulate slot\n", result) ;

            goto sim_error ;
          }

          break ;
        }

        /**********************************************************************/

      case TDICE_SEND_OUTPUT :
        {
          OutputInstant_t  instant ;
          OutputType_t     type ;
          OutputQuantity_t quantity ;
          //clock_t Time;          

          extract_message_word (&request, &instant,  0) ;
          extract_message_word (&request, &type,     1) ;
          extract_message_word (&request, &quantity, 2) ;

          network_message_init (&reply) ;
          build_message_head   (&reply, TDICE_SEND_OUTPUT) ;

          float   time = get_simulated_time (&analysis) ;
          Quantity_t n = get_number_of_inspection_points (&output, instant, type, quantity) ;

          insert_message_word (&reply, &time) ;
          insert_message_word (&reply, &n) ;

          if (n > 0)
          {
            error = fill_output_message

              (&output, stkd.Dimensions,
               tdata.Temperatures, tdata.PowerGrid.Sources,
               instant, type, quantity, &reply) ;

            if (error != TDICE_SUCCESS)
            {
              fprintf (stderr, "error: generate message content\n") ;

              network_message_destroy (&reply) ;

              goto sim_error ;
            }
          }
          if(type == TDICE_OUTPUT_TYPE_TMAP && compressionUsed)
          {
            //init histTableMap tailMap for compression
            //New packet tmapReply is being initialised /
            //clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t1);
            network_message_init (&tmapReply);
            build_message_head (&tmapReply, TDICE_SEND_OUTPUT);

            insert_message_word (&tmapReply, &time);
            insert_message_word (&tmapReply, &n);

            CellIndex_t nrows, ncols;

            int readIndex = 4;

            //get the no of rows and cols from reply packet
            extract_message_word (&reply, &nrows, 2);
            extract_message_word (&reply, &ncols, 3);

            //insert nrows and ncols into my packet
            insert_message_word (&tmapReply, &nrows);
            insert_message_word (&tmapReply, &ncols);

            int ret=-1, f1, f2;
            unsigned int compressedWord, i, j, k, l;
            unsigned int flag=0, intPart, decPart;

            unsigned int tmp[3];
            float temper;

            for(i=0; i<n; i++)
              for(j=0; j<nrows; j++)
                for(k=0; k<ncols; k++)
                {
                  f1 = -1;f2 = -1;

                  extract_message_word (&reply, &temper, readIndex);
                  readIndex++;
                  unsigned int intPartTemper, decPartTemper;
                  getParts (temper, &intPartTemper, &decPartTemper);
                  for (l=0;l<HIST_TABLE_SIZE;l++)
                  {
                    if(histTableMap[i][j][k][l] != -1.0)
                    {
                      getParts (histTableMap[i][j][k][l], &intPart, &decPart);
                      if(intPart == intPartTemper)f1 = l;
                      float val=((float)decPart-decPartTemper);
                      val=val/(float)pow(10,DECIMAL_DIGITS);
                      val=abs (val);
                      if(val <= TOLERANCE)
                        f2=l;
                    }
                  }
                  flag = getFlag (f1,f2);

                  switch (flag)
                  {
                    case 1:
                      histTableMap[i][j][k][tailMap[i][j][k]] = intPartTemper + 
                        (histTableMap[i][j][k][f2] - (int)histTableMap[i][j][k][f2]);
                      tailMap[i][j][k] = (tailMap[i][j][k] + 1)%HIST_TABLE_SIZE;
                      break;

                    case 2:
                      histTableMap[i][j][k][tailMap[i][j][k]] = (int)histTableMap[i][j][k][f1] + 
                        ((float)decPartTemper)/(pow(10,DECIMAL_DIGITS));
                      tailMap[i][j][k] = (tailMap[i][j][k] + 1)%HIST_TABLE_SIZE;
                      break;

                    case 3:
                      histTableMap[i][j][k][tailMap[i][j][k]] = intPartTemper + 
                        ((float)decPartTemper)/(pow(10,DECIMAL_DIGITS));
                      tailMap[i][j][k] = (tailMap[i][j][k] + 1)%HIST_TABLE_SIZE;
                      break;

                    default:
                      break;
                  }
                  getArgsInArray (f1, f2, flag, intPartTemper, decPartTemper, tmp);
                  ret = getNextCompressedWord (tmp[1], tmp[2], flag, &compressedWord, !WRAP_UP);
                  if(ret != WORD_INCOMPLETE)
                    insert_message_word (&tmapReply, &compressedWord);
                }
            switch(ret)
            {
              case WORD_INCOMPLETE:
              case WORD_IN_TEMP:
                getNextCompressedWord (tmp[1], tmp[2], flag, &compressedWord, WRAP_UP);
                insert_message_word (&tmapReply, &compressedWord);
                break;
              default:
                break;
            }
            clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t1);
            send_message_to_socket (&client_socket, &tmapReply);
            clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t2);
            diff(t1,t2,&res); 
            network_message_destroy (&reply);
            network_message_destroy (&tmapReply);
          }
          else
          {
            if(type == TDICE_OUTPUT_TYPE_TMAP)
            {
            //Time = clock();            
            clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t1);
            send_message_to_socket (&client_socket, &reply);
            //timeForTmap +=  ((double)clock() - Time) / CLOCKS_PER_SEC  ;
            clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t2);
            diff(t1,t2,&res); 
            }
            else
            send_message_to_socket (&client_socket, &reply) ;

            network_message_destroy (&reply) ;
          }
          break ;
        }

        /**********************************************************************/

      case TDICE_PRINT_OUTPUT :
        {
          OutputInstant_t  instant ;

          extract_message_word (&request, &instant,  0) ;

          if (headers == false)
          {
            Error_t error = generate_output_headers

              (&output, stkd.Dimensions, (String_t)"% ") ;

            if (error != TDICE_SUCCESS)
            {
              fprintf (stderr, "error in initializing output files \n ");

              goto sim_error ;
            }

            headers = true ;
          }

          generate_output

            (&output, stkd.Dimensions,
             tdata.Temperatures, tdata.PowerGrid.Sources,
             get_simulated_time (&analysis), instant) ;

          break ;
        }

        /**********************************************************************/

      case TDICE_SIMULATE_SLOT :
        {
          network_message_init (&reply) ;
          build_message_head   (&reply, TDICE_SIMULATE_SLOT) ;

          SimResult_t result = emulate_slot (&tdata, stkd.Dimensions, &analysis) ;

          insert_message_word (&reply, &result) ;

          send_message_to_socket (&client_socket, &reply) ;

          network_message_destroy (&reply) ;

          if (result == TDICE_END_OF_SIMULATION)
          {
            network_message_destroy (&request) ;

            goto quit ;
          }
          else if (result != TDICE_SLOT_DONE)
          {
            fprintf (stderr, "error %d: emulate slot\n", result) ;

            goto sim_error ;
          }

          break ;
        }

        /**********************************************************************/

      case TDICE_SIMULATE_STEP :
        {
          network_message_init (&reply) ;
          build_message_head   (&reply, TDICE_SIMULATE_STEP) ;

          SimResult_t result = emulate_step (&tdata, stkd.Dimensions, &analysis) ;

          insert_message_word (&reply, &result) ;

          send_message_to_socket (&client_socket, &reply) ;

          network_message_destroy (&reply) ;

          if (result == TDICE_END_OF_SIMULATION)
          {
            network_message_destroy (&request) ;

            goto quit ;
          }
          else if (result != TDICE_STEP_DONE && result != TDICE_SLOT_DONE)
          {
            fprintf (stderr, "error %d: emulate step\n", result) ;

            goto sim_error ;
          }

          break ;
        }

        /**********************************************************************/

      default :

        fprintf (stderr, "ERROR :: received unknown message type") ;
    }

    network_message_destroy (&request) ;

  } while (1) ;

  /**************************************************************************/

quit :

  socket_close              (&client_socket) ;
  socket_close              (&server_socket) ;
  thermal_data_destroy      (&tdata) ;
  stack_description_destroy (&stkd) ;
  output_destroy            (&output) ;

  printf("Time taken %d sec %d nsec \n",(int)res.tv_sec,(int)res.tv_nsec);
  return EXIT_SUCCESS ;

sim_error :
  network_message_destroy   (&request) ;
  socket_close              (&client_socket) ;
wait_error :
  socket_close              (&server_socket) ;
socket_error :
  thermal_data_destroy      (&tdata) ;
ftd_error :
wrong_analysis_error :
  stack_description_destroy (&stkd) ;
  output_destroy            (&output) ;

  return EXIT_FAILURE ;
}