Exemple #1
0
bool OsFile_UnmapImage(OsFile_MappedImageHandle mapped_image) {
  bool ok = true;
  Linux_MappedImage *img = (Linux_MappedImage*) mapped_image;

  if (jvm_munmap(img->mapped_address, img->ro_length) != 0) {
    ok = false;
  }
  if (img->rw_mapped_address != (address)-1 && 
      jvm_munmap(img->rw_mapped_address, img->rw_length) != 0) {
    ok = false;
  }
  jvm_free(mapped_image);

  return ok;
}
Exemple #2
0
/*****************************************************************************
 * FUNCTION
 *  jam_listmem_free
 * DESCRIPTION
 *  
 * PARAMETERS
 *  ptr     [?]     [?]
 * RETURNS
 *  void
 *****************************************************************************/
void jam_listmem_free(void *ptr)
{
#ifndef J2ME_SLIM_REMOVE_LIST_MEMORY_SUPPORT
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    listmem_hdr header;
    char *mem_pool_start = g_jam_midslist_mem_start_p;
    char *mem_pool_end = g_jam_midslist_mem_end_p;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (ptr == NULL)
    {
        kal_trace(TRACE_INFO, FUNC_JAM_LIST_FREE_NULL);
    }
    else if (((char*)ptr > mem_pool_end) || ((char*)ptr < mem_pool_start))
    {
        kal_trace(TRACE_INFO, FUNC_JAM_LIST_FREE_OUT_OF_SCOPE, (kal_int32) ptr);
    }
    else
    {
        header = (listmem_hdr) ((char*)ptr - sizeof(listmem_struct));

        if (header->magic != MAGIC)
        {
            kal_trace(TRACE_INFO, FUNC_JAM_LIST_FREE_CORRUPTED_MEM, (kal_int32) ptr);
        }
        else if (header->free != 0)
        {
            kal_trace(TRACE_INFO, FUNC_JAM_LIST_FREE_TWICE, (kal_int32) ptr);
        }
        else
        {
            header->free = 1;
        }
    }
#else
    kal_prompt_trace(MOD_JAM, "Here used to be list memory free.");
    jvm_free(ptr);     
#endif	
}
void jvm_StackFree(JVMStack *stack) {
  jvm_free(stack->data);
  jvm_free(stack->flags);
}
Exemple #4
0
void OsMemory_free(void *p) {
  jvm_free(p);
}
Exemple #5
0
OsFile_MappedImageHandle OsFile_MapImage(const PathChar* name,
                                         address preferrable_destination,
                                         int length, int rw_offset,
                                         int rw_length) {
  GUARANTEE(length == rw_offset + rw_length, "assumption");
  GUARANTEE((int(preferrable_destination) % ASSUMED_PAGE_SIZE) == 0,
            "page aligned");
#if USE_UNICODE_FOR_FILENAMES
  char ascii_name[256];
  int len = fn_strlen(name);
  if (len > 255) {
    len = 255;
  }
  for (int i=0; i<len; i++) {
    ascii_name[i] = (char)name[i];
  }
  ascii_name[len] = 0;
#else
  const char *ascii_name = name;
#endif
  Linux_MappedImage *img = 
      (Linux_MappedImage*)jvm_malloc(sizeof(Linux_MappedImage));

  if (img == NULL) {
    return NULL;
  }

  if (Verbose) {
    TTY_TRACE_CR(("Map image desired = 0x%x",int(preferrable_destination))); 
  }
  
  int open_flags = O_RDONLY;
  int fd = jvm_open(ascii_name, open_flags);

  // Align the RW region down so that the RW region starts at
  // a page boundary.
  int ro_length = length - rw_length;
  ro_length = (int)align_size_down(ro_length, ASSUMED_PAGE_SIZE);
  rw_offset = ro_length;
  rw_length = length - ro_length;

  if (ForceImageRelocation) {
    // For testing purposes, check relocations
    preferrable_destination += ASSUMED_PAGE_SIZE * 17;
  }

  address ro_addr = (address)-1;
  address rw_addr = (address)-1;
  address ro_preferred = preferrable_destination;
  address rw_preferred = preferrable_destination + rw_offset;

  if (fd == -1) {
    goto error;
  }


  if (ro_length > 0 && LoadXIPImage && !ForceImageRelocation) {
    ro_addr = (address)jvm_mmap(ro_preferred, ro_length, 
                                PROT_READ, MAP_PRIVATE, fd, 0);
    rw_addr = (address)jvm_mmap(rw_preferred, rw_length, 
                                PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,
                                rw_offset);
  } else {
    // The whole image needs to be mapped R/W.
  }

  if (ro_addr == ro_preferred && rw_preferred == rw_preferred) {
    if (Verbose) {
      TTY_TRACE_CR(("Map image actual  = 0x%x [RO] size=%d", int(ro_addr),
                                                             ro_length));
      TTY_TRACE_CR(("Map image actual  = 0x%x [RW] size=%d", int(rw_addr),
                                                             rw_length));
    }
  
    img->mapped_address    = ro_addr;
    img->ro_length         = ro_length;
    img->rw_mapped_address = rw_addr;
    img->rw_length         = rw_length;
  } else {
    // Can't get to our preferred location. Relocation of the image content
    // is needed, so we need to remap the whole thing using RW mapping
    if (ro_addr != (address) -1) {
      jvm_munmap(ro_addr, ro_length);
    }
    if (rw_addr != (address) -1) {
      jvm_munmap(rw_addr, rw_length);
    }
    ro_addr = (address)jvm_mmap(ro_preferred, length,
                                PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
    if (ro_addr == (address)-1) {
      goto error;
    }

    if (Verbose) {
      TTY_TRACE_CR(("Map image actual  = 0x%x [RW] size=%d", int(ro_addr),
                                                             length));
    }
  
    img->mapped_address    = ro_addr;
    img->ro_length         = length;
    img->rw_mapped_address = (address)-1;
    img->rw_length         = 0;
  }

  // we don't need fd anymore, mapping is still preserved
  jvm_close(fd);
  return img;

error:
  if (img) {
    jvm_free(img);
  }
  if (fd >= 0) {
    jvm_close(fd);
  }
  return NULL;
}
Exemple #6
0
/*****************************************************************************
 * FUNCTION
 *  jbt_cmd_check_device_in_queue
 * DESCRIPTION
 *  
 * PARAMETERS
 *  cmd_opcode      [IN]        
 * RETURNS
 *  
 *****************************************************************************/
kal_bool jbt_cmd_check_device_in_queue(kal_uint8 cmd_opcode)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    JBT_cmd_node *node = 0;
    kal_bool status = KAL_FALSE;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    kal_trace(TRACE_FUNC, FUNC_JBT_RESULT, 0xB1);
    kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_CHECK_DEVICE_IN_QUEUE_CMD_OPCODExD, cmd_opcode);

    node = jbt_cmd_check_and_remove_in_queue(cmd_opcode);
    if (node != 0)
    {
        kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_CHECK_DEVICE_IN_QUEUE_NODE_NOT_0);
        switch (cmd_opcode)
        {
            case JBT_CMDCODE_DEVICE_DISCOVERY_CMD:
            {
                bt_bm_discovery_cancel_cnf_struct *msg;

                msg = (bt_bm_discovery_cancel_cnf_struct*)
                    construct_local_para(sizeof(bt_bm_discovery_cancel_cnf_struct), TD_UL);

                msg->result = BTBM_ADP_SUCCESS;
                msg->total_number = 0;
                jbt_send_msg(
                    MSG_ID_BT_BM_DISCOVERY_CANCEL_CNF,
                    MOD_JASYN,
                    MOD_JASYN,
                    BT_APP_SAP,
                    (local_para_struct*) msg,
                    NULL);

            }

                break;
            case JBT_CMDCODE_GET_REMOTE_NAME:
            {
                bt_bm_read_remote_name_cnf_struct *msg;
                jbt_gap_get_remote_name *info;

                info = (jbt_gap_get_remote_name*) node->parms;
                msg = (bt_bm_read_remote_name_cnf_struct*)
                    construct_local_para(sizeof(bt_bm_read_remote_name_cnf_struct), TD_UL);
                msg->result = BTBM_ADP_FAILED;
                msg->name_len = 0;
                memset(msg->name, 0, BTBM_ADP_MAX_NAME_LEN);
                msg->cod = 0;
                jbt_ConvertToBdAddr(&(msg->bd_addr), info->addr);
                jbt_send_msg(
                    MSG_ID_BT_BM_READ_REMOTE_NAME_CNF,
                    MOD_JASYN,
                    MOD_JASYN,
                    BT_APP_SAP,
                    (local_para_struct*) msg,
                    NULL);

            }
                break;
            case JBT_CMDCODE_SET_LOCAL_COD:
            {
                bt_bm_write_local_cod_cnf_struct *msg;

                msg = (bt_bm_write_local_cod_cnf_struct*)
                    construct_local_para(sizeof(bt_bm_write_local_cod_cnf_struct), TD_UL);
                msg->result = BTBM_ADP_FAILED;
                jbt_send_msg(
                    MSG_ID_BT_BM_WRITE_LOCAL_COD_CNF,
                    MOD_JASYN,
                    MOD_JASYN,
                    BT_APP_SAP,
                    (local_para_struct*) msg,
                    NULL);

            }
                break;
            case JBT_CMDCODE_SET_ACL_SECURITY_INFO:
            {
                bt_jsr82_set_acl_security_cnf_struct *msg;
                jbt_gap_set_security *info = (jbt_gap_set_security*) node->parms;

                msg = (bt_jsr82_set_acl_security_cnf_struct*)
                    construct_local_para(sizeof(bt_jsr82_set_acl_security_cnf_struct), TD_UL);
                msg->result = BTBM_ADP_FAILED;
                msg->transaction_id = info->transaction_id;
                memcpy(msg->bdAddr, info->addr, 6);
                jbt_send_msg(
                    MSG_ID_BT_JSR82_SET_ACL_SECURITY_CNF,
                    MOD_JASYN,
                    MOD_JASYN,
                    BT_APP_SAP,
                    (local_para_struct*) msg,
                    NULL);

            }
                break;
            default:
                ASSERT(0);
        }
        kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_CHECK_DEVICE_IN_QUEUE_NODEPARMS_08X, node->parms);
        if (node->parms != 0)
        {
            jbt_free_record(node->parms);
#ifndef JBT_SUPPORT_ADM_MEM
            jvm_free(node->parms);
#else
            kal_adm_free(jbt_get_mem_pool_id(),node->parms);
#endif
        }
        node->parms = 0;
        node->cmd_code = 0;
        kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_CHECK_DEVICE_IN_QUEUE_JBT_CMD_LIST_INIT);
        jbt_cmd_list_init(node);
        kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_CHECK_DEVICE_IN_QUEUE_JBT_CMD_LIST_INSERT_TAIL);
        jbt_cmd_list_insert_tail(&jbt_cmd_queue_context.empty_list, node);
        status = KAL_TRUE;
    }
    return status;
}
Exemple #7
0
/*****************************************************************************
 * FUNCTION
 *  jbt_cmd_queue_nonconcurrent_processing
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void jbt_cmd_queue_nonconcurrent_processing(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    JBT_cmd_node *node;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    kal_take_sem(jbt_cmd_process_sem, KAL_INFINITE_WAIT);

    kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_QUEUE_NONCONCURRENT_PROCESSING);

    if (jbt_cmd_queue_context.cmd_run_no < JBT_MAX_COMMAND_NO)
    {
        kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_LIST_REMOVE_HEAD);
        node = jbt_cmd_list_remove_head(&jbt_cmd_queue_context.cmd_list);
        jbt_cmd_queue_context.cmd_run_no++;
        kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_QUEUE_CONTEXTCMD_RUN_NOxD, jbt_cmd_queue_context.cmd_run_no);
        kal_trace(TRACE_JBT_GROUP, JBT_NODECMD_CODExD, node->cmd_code);
        switch (node->cmd_code)
        {
            case JBT_CMDCODE_DEVICE_RESTORE_COD:
                jbt_set_local_cod_cmd_body(node->parms, BTBM_WRITE_COD_RESTORE);
                break;
            case JBT_CMDCODE_DEVICE_RESTORE_MMI:
                break;
            case JBT_CMDCODE_SET_LOCAL_COD:
                jbt_set_local_cod_cmd_body(node->parms, BTBM_WRITE_COD_MASK);
                break;
            case JBT_CMDCODE_GET_REMOTE_NAME:
                jbt_get_remote_name_cmd_body(node->parms);
                break;
            case JBT_CMDCODE_DEVICE_DISCOVERY_CMD:
                jbt_device_discovery_cmd_body(node->parms);
                break;
                /* Add 2007-1119 */
            case JBT_CMDCODE_SET_ACL_SECURITY_INFO:
                jbt_set_acl_security_cmd_body(node->parms);
                break;
            default:
                ASSERT(0);
                break;
        }
        if (node->parms != 0)
        {
            jbt_free_record(node->parms);
#ifndef JBT_SUPPORT_ADM_MEM
            jvm_free(node->parms);
#else
            kal_adm_free(jbt_get_mem_pool_id(),node->parms);
#endif
        }
        node->parms = 0;
        node->cmd_code = 0;
        kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_LIST_INIT);
        jbt_cmd_list_init(node);
        kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_LIST_INSERT_TAIL);
        jbt_cmd_list_insert_tail(&jbt_cmd_queue_context.empty_list, node);

    }
    kal_give_sem(jbt_cmd_process_sem);
    kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_QUEUE_NONCONCURRENT_PROCESSING_END);

}
Exemple #8
0
/*****************************************************************************
 * FUNCTION
 *  jbt_cmd_handle_restore
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void jbt_cmd_handle_restore(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint8 i = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    kal_trace(TRACE_FUNC, FUNC_JBT_RESULT, 0xA1);

    /* TODO the check range may be furthur optimized. we only need to check specific non-concurrent commands */
    for (i = JBT_CMDCODE_GET_LOCAL_NAME; i < JBT_CMDCODE_DEVICE_DISCOVERY_CMD_CANCEL; i++)
    {
        kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_HANDLE_RESTORE_INDEXx02X, i);
        jbt_cmd_check_device_in_queue(i);
    }
    kal_trace(
        TRACE_JBT_GROUP,
        JBT_JBT_CMD_HANDLE_RESTORE_JBT_CMD_QUEUE_CONTEXTCMD_RUN_NOx02X,
        jbt_cmd_queue_context.cmd_run_no);

    if (jbt_cmd_queue_context.cmd_run_no == 0)
    {
        jbt_restore_procedure(JBT_CMD_SUCCESS);
    }
    else
    {
        /* Add check: If JBT is in POWER_OTHERS state (caused by VM finalize), if there is an inquiry ongoing, just cancels it */
        if (jbt_gap_power_state() == JBT_POWER_OTHERS)
        {
            jbt_op_info *discovery_operation = jbt_gap_obtain_operation(JBT_CMDCODE_DEVICE_DISCOVERY_CMD);

            if (discovery_operation->operation_state == JBT_OPERATION_DISCOVERY_SEARCHING)
            {
                /* Existing an ongoing discovery operation, sends discovery cancel cmd to btstack to speed the process of VM termination */
                jbt_gap_device_discovery_cancel *parms;
                kal_uint32 jbt_gap_device_discovery_cancel_size = sizeof(jbt_gap_device_discovery_cancel);
                kal_uint8 block_inx = 0;
                
                /*
                 * DONT invoke the function jbt_device_discovery_cancel_cmd() directly:
                 * Because JBT is not power_on status during VM termination, if invoking this function will return failure
                 */
                block_inx = jbt_malloc_block(jbt_gap_device_discovery_cancel_size);

#ifndef JBT_SUPPORT_ADM_MEM
                parms = (jbt_gap_device_discovery_cancel*) jvm_malloc(jbt_gap_device_discovery_cancel_size);
#else                
                parms = (jbt_gap_device_discovery_cancel*) kal_adm_alloc(jbt_get_mem_pool_id(),jbt_gap_device_discovery_cancel_size);
#endif
                if (parms != 0)
                {
                    kal_trace(TRACE_JBT_GROUP, FUNC_JBT_CANCEL_INQUIRY_DUE_TO_VM_TERMINATE);
                    jbt_malloc_record(block_inx, (kal_uint8*)parms, jbt_gap_device_discovery_cancel_size);
                    parms->transaction_id = discovery_operation->transaction_id;
                    kal_trace(TRACE_JBT_GROUP, JBT_JBT_CMD_INSERT_QUEUE);
                    jbt_cmd_insert_queue(
                        JBT_CMDCODE_DEVICE_DISCOVERY_CMD_CANCEL,
                        JBT_STACK_CMD_CONCURRENT,
                        (kal_uint8*) parms);
                    
                    jbt_free_record((kal_uint8*) parms);
#ifndef JBT_SUPPORT_ADM_MEM
                    jvm_free((kal_uint8*) parms);
#else
                    kal_adm_free(jbt_get_mem_pool_id(),(kal_uint8*) parms);
#endif
                }
                else
                {
                    /* Failed of jvm_malloc() */
                    jbt_free_block(block_inx);
                    kal_trace(TRACE_JBT_GROUP, FUNC_JBT_CANCEL_INQUIRY_FAILED_WHILE_VM_TERMINATE);
                }
            }
        }
    }
}
int main(int argc, char *argv[])
{
  uint8                 *buf;
  JVMMemoryStream       m;
  JVMClass              *jclass;
  JVMBundle             jbundle;
  JVMBundleClass        *jbclass;
  JVM                   jvm;
  JVMObject             *jobject;
  JVMLocal              locals[10];
  uint32                size;
  int                   result;
  JVMLocal              jvm_result;
  JVMLocal              _result;
  int                   x;
  uint8                 *entryClass;
  uint8                 *utf8;

  uint8                 o;
  uintptr               n;
  void                  *p;
  
  jvm.objects = 0;
  jvm.cmark = 0;
  jvm.mutex = 0;
  jbundle.first = 0;
  jvm.bundle = &jbundle;
  
  //x = jvm_GetMethodTypeArgumentCount("(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
  //jvm_printf("argcnt:%u\n", x);
  
  // if the internal malloc is enabled we simply just hand it
  // a rather large chunk of memory to use
  #ifdef INTERNALMALLOC
  p = malloc(1024 * 1024 * 8);
  jvm_m_give(p, 1024 * 1024 * 8);
  #endif

  buf = jvm_ReadWholeFile("./ert/sys/Core.class", &size);
  msWrap(&m, buf, size);
  jclass = jvm_LoadClass(&m);
  jclass->flags = JVM_CLASS_NATIVE;
  jclass->nhand = jvm_core_core_handler;
  jvm_free(buf);
  jvm_AddClassToBundle(&jbundle, jclass);
  
  entryClass = 0;
  for (x = 1; x < argc; ++x) {
    if (argv[x][0] == ':') {
      // holds classpath and class name for entry
      entryClass = &argv[x][1];
    } else {
      debugf("@@>loading %s\n", argv[x]);
      buf = jvm_ReadWholeFile(argv[x], &size);
      msWrap(&m, buf, size);
      jclass = jvm_LoadClass(&m);
      jvm_free(buf);
      debugf("@@>jclass: %lx\n", jclass);
      jvm_AddClassToBundle(&jbundle, jclass);
    }
  }

  // make static fields for all classes in bundle,
  // also this calls the special <clinit>:()V method
  debugf("here\n");
  jvm_MakeStaticFieldsOnBundle(&jvm, &jbundle);
  debugf("here\n");
  
  // catch common mistake and present user with something meaningful
  if (!entryClass) {
    debugf("No entry class specified on command line with :<entryclass>\n");
    return -1;
  }
  
  /// create initial object
  result = jvm_CreateObject(&jvm, &jbundle, entryClass, &jobject);
  jclass = jvm_FindClassInBundle(&jbundle, entryClass);

  if (!jobject) {
    debugf("could not create object?\n");
    jvm_exit(-1);
  }

  locals[0].data = (uint64)jobject;
  locals[0].flags = JVM_STACK_ISOBJECTREF;
  jvm_result.data = 0;
  jvm_result.flags = 0;
  result = jvm_ExecuteObjectMethod(&jvm, &jbundle, jclass, "main", "()J", &locals[0], 1, &jvm_result);
  if (result < 0) {
    if (!jvm_result.data) {
      debugf("error occured too soon; error-code:%i\n", result);
      exit(-1);
    }
    // the exception should be stored in jvm_result
    debugf("exception code:%i\n", result);
    debugf("jvm_result.data:%x jvm_result.flags:%x\n", jvm_result.data, jvm_result.flags);
    // walk the stack
    errorf("-------- UNCAUGHT EXCEPTION ---------\n");
    errorf("  %s\n", jvm_GetClassNameFromClass(((JVMObject*)jvm_result.data)->class));
    jvm_GetField((JVMObject*)jvm_result.data, "msg", &_result);
    debugf("here %x\n", _result.data);
    jvm_GetString((JVMObject*)_result.data, &utf8);
    errorf(" msg:%s\n", utf8);
    jvm_GetField((JVMObject*)jvm_result.data, "code", &_result);
    errorf(" code:%i\n", _result.data);
    result = jvm_GetField((JVMObject*)jvm_result.data, "first", &_result);
    while (_result.data != 0) {
      result = jvm_GetField((JVMObject*)_result.data, "methodName", &jvm_result);
      jvm_GetString((JVMObject*)jvm_result.data, &utf8);
      errorf("    method:%s", utf8);
      result = jvm_GetField((JVMObject*)_result.data, "className", &jvm_result);
      jvm_GetString((JVMObject*)jvm_result.data, &utf8);
      errorf(" class:%s", utf8);
      result = jvm_GetField((JVMObject*)_result.data, "methodType", &jvm_result);
      jvm_GetString((JVMObject*)jvm_result.data, &utf8);
      errorf(" type:%s", utf8);
      result = jvm_GetField((JVMObject*)_result.data, "opcodeIndex", &jvm_result);
      errorf(" opcode:%u\n", jvm_result.data);
      //result = jvm_GetField((JVMObject*)_result.data, "sourceLine", &jvm_result);
      //debugf("sourceLine:%u\n", jvm_result.data);
      // get next item, if any
      debugf("    -----\n");
      result = jvm_GetField((JVMObject*)_result.data, "next", &_result);
    }
    debugf("calling collect.. %x\n", jobject);
    jvm_Collect(&jvm);
    return -1;
  }

  jvm_printf("done! result.data:%i result.flags:%u\n", jvm_result.data, jvm_result.flags);

  jvm_Collect(&jvm);

  return 1;
}